This article mainly for you to share the PHP file upload class related code, with a certain reference value, interested in small partners can refer to
This article example for everyone to share the PHP file upload class specific code for your reference, the specific content as follows
<?php$upload = new Upload (); $upload->uploadfile (' FM ');/* Print error message *///var_dump ($upload->errornumber);//Var_ Dump ($upload->errorinfo); class upload{//File upload path protected $path = ' upload/'; Allow file upload suffix protected $allowSuffix = [' jpg ', ' jpeg ', ' gif ', ' wbmp ', ' PNG ']; MIME type protected $allowMime =[' image/jpg ', ' image/jpeg ', ' image/gif ', ' image/wbmp ', ' image/png ']; Allowed upload size protected $maxSize = 2000000; Whether to enable the default prefix protected $isRandName =true; The file prefix protected $prefix = ' up_ '; Error number and error message protected $errorNumber; protected $errorInfo; File information//filename protected $oldName; The suffix of the file protected $suffix; The size of the file protected $size; The MIME protected of the file $mime; The path to the file's temporary file protected $tmpName; File new name protected $newName; Construction method//Because the member property is more than the array to display the public function __construct ($arr =[]) {foreach ($arr as $key + $value) {$this->se Toption ($key, $value); }}//Judge $key is not my member property, if it is set protected function setOption ($key, $value) {//Get all member properties $keys = Array_Keys (Get_class_vars (__class__)); if (In_array ($key, $keys)) {$this, $key = $value; }}//File Upload function//key is the value of the Name property in the Input box public function UploadFile ($key) {//To determine if there is no set path to the paths if (empty ($this->path) ) {$this->setoption (' errornumber ',-1); return false; }//Determine if the path exists if writable if (! $this->check ()) {$this->setoption (' errornumber ',-2); return false; }//To determine if the error message inside the $_files is 0, if 0 then the file information can be obtained directly on the server side, the extracted information is saved to the member properties $error = $_files[$key [' Error ']; if ($error) {$this->setoption (' ErrorNumber ',-3); return false; }else {//Extract file-related information and save to member properties $this->getfileinfo ($key); }//Determine if the file size, mime, and suffix are in accordance with if (! $this->checksize () | |! $this->checkmime () | |! $this->checksuffix ()) {Retu RN false; }//Get new file name $this->newname = $this->createnewname (); Determine if the file is being uploaded and is moving the upload file if (is_uploaded_file ($this->tmpname)) {if (Move_uploaded_file ($this->tmpname, $this-> ;p Ath. $this-≫newname) {return $this->path. $this->newname; }else {$this->setoption (' errornumber ',-7); return false; }}else{$this->setoption (' errornumber ',-6); return false; }}//detects if a folder exists, is writable protected function check () {//folder does not exist or is not a directory. Create Folder if (!file_exists ($this->path) | |! Is_dir ($this->path)) {return mkdir ($this->path,0777,true); }//Determine if the file is writable if (!is_writeable ($this->path)) {return chmod ($this->path, 0777); } return true; }//Get file information according to key protected function GetFileInfo ($key) {//Get the name of the file $this->oldname = $_files[$key] [' name ']; Get the MIME type of the file $this->mime = $_files[$key [' type ']; Temporary documents obtained $this->tmpname = $_files[$key] [' tmp_name ']; Get file size $this->size = $_files[$key [' size ']; Get the file suffix $this->suffix = pathinfo ($this->oldname) [' extension ']; }//Determine file size protected function Checksize () {if ($this->size > $this->maxsize) {$This->setoption (' ErrorNumber ',-3); return false; } return true; }//To determine the MIME type protected function Checkmime () {if (!in_array ($this->mime, $this->allowmime)) {$this->setop tion (' errornumber ',-4); return false; } return true; }//Judgment suffix protected function checksuffix () {if (!in_array ($this->suffix, $this->allowsuffix)) {$this->set Option (' ErrorNumber ',-5); return false; } return true; }//Create new name protected function Createnewname () {if ($this->israndname) {$name = $this->prefix.uniqid (). $this->suffix; }else {$name = $this->prefix. $this->oldname; } return $name; Public Function __get ($name) {if ($name = = ' ErrorNumber ') {return $this->errornumber; }elseif ($name = = ' ErrorInfo ') {return $this->geterrorinfo (); }} protected function GetErrorInfo () {switch ($this->errornumber) {case-1: $str = ' file path not set '; Break Case-2: $str= ' file is not a directory or not writable '; Break Case-3: $str = ' file exceeds the specified size '; Break Case-4: $str = ' MIME type not compliant '; Break Case-5: $str = ' file suffix not compliant '; Break Case-6: $str = ' not upload file '; Break Case-7: $str = ' move failed '; Break Case 1: $str = ' exceeds ini set size '; Break Case 2: $str = ' exceeds HTML form size '; Break Case 3: $str = ' article only partially uploaded '; Break Case 4: $str = ' no file upload '; Break Case 6: $str = ' temporary file not found '; Break Case 7: $str = ' file write Failed '; Break } return $STR; }}
<!doctype html>
Note:the name in input must be the same as the value in the UploadFile in the upload Class!
For more information, please refer to the php file upload operation summary for learning.