PHP Verification Code Generation class

Source: Internet
Author: User

<?php
Class Upload {
Determining properties
Private $_max_size;
Private $_ext_list= Array ('. jpg ', '. png ', '. gif ');
Private $_allow_mine_list= Array (' IMAGE/PGN ', ' image/gif ', ' image/jpeg ', ' image/pjpeg ', ' image/x-png ');
Private $_upload_path;
Private $_prefix;
Public Function __construct ()
{
Setmaxsize, $this (4*1024*1024);
}
The corresponding set method is given.
Public Function setmaxsize ($max _size) {
$this->_max_size= $max _size;
}

Public function setextlist (array $ext _list = Array ('. jpg ', '. png ', '. gif ')) {
$this->_ext_list= $ext _list;
}

Public function setallowmimelist (array $allow _mine_list=array (' image/pgn ', ' image/gif ', ' image/jpeg ', ' image/pjpeg ', ' Image/x-png ')) {
$this->_allow_mine_list= $allow _mine_list;
}

Public Function Setuploadpath ($upload _path) {
$this->_upload_path= $upload _path;
}
Public Function Setprefix ($prefix) {
$this->_prefix= $prefix;
}

Public Function Doupload ($tmp _file) {
Determine the file size, when the file is too large to give a hint, discard the upload
if ($tmp _file[' size ']> $this->_max_size) {
Echo ' The file you uploaded is too large ';
return false;
}
if (! ( $tmp _file[' ERROR ']===0)} {
Echo ' Error uploading file ';
return false;
}
Add a piece of code to verify that the uploaded file type is correct
The suffix of the uploaded file is uniformly converted to lowercase
$ext = Strtolower (STRRCHR ($tmp _file[' name '], '. '));

if (!in_array ($ext, $this->_ext_list)) {
Echo ' You uploaded the wrong file type ';
return false;
}
Second-level protection of files, verifying the mime of uploaded files
$mime _type = $tmp _file[' type ');
if (!in_array ($mime _type, $this->_allow_mine_list)) {
Echo ' MIME of the file you uploaded is incorrect ';
return false;
}
Third-level protection against file types, mime detection of file types using PHP programs
In order to use the Finfo class, you need to open an extension in php.ini: extension php_fileinfo.dll
$finfo = new Finfo (fileinfo_mime_type);
$mime _type= $finfo->file ($tmp _file[' tmp_name ');
if (!in_array ($mime _type, $this->_allow_mine_list)) {
Echo ' type is not legal ';
return false;
}
Here we add a piece of code to make the file name unique
$filename = uniqid ($this->_prefix,true);
Stitching up a full and unique file name
$upload _filename= $filename. $ext;

Add sub-catalogue storage processing, remember to take the last
$sub _dir = Date (' Ymd '). ' /‘;
Determine if this directory exists
if (!is_dir ($this->_upload_path. $sub _dir)) {
If the directory does not exist, create a new
mkdir ($this->_upload_path. $sub _dir,0777,true);
}
if (Move_uploaded_file (Iconv (' GBK ', ' utf-8 ', $tmp _file[' Tmp_name '), Iconv (' Utf-8 ', ' GBK ', $this->_upload_path.$ Sub_dir. $upload _filename))) {
Return $sub _dir. $upload _filename;
}else{
return false;
}
}
}



This article from the "10988062" blog, reproduced please contact the author!

PHP Verification Code Generation class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.