<?PHP/** *================================================================== * upload.class.php file Upload class for file upload function * @ Author Wang Suping * @copyright Preach Wisdom Podcast PHP Academy 2006-2014 * @version 1.0 * March 27, 2013 0:37:15 *========================================= ========================= */classupload{Private $path;//File upload directory Private $max _size;//upload file size limit Private $errno;//error message number Private $mime=Array(' image/jpeg ', ' image/png ', ' image/gif ');//allowed file types to upload /** constructor, * @access public * @param $path the path of string upload*/ public function__construct ($path=Upload_path) { $this->path =$path; $this->max_size = 1000000; } /** * File Upload method, sub-directory storage file * @access public * @param $file array containing the information of the uploaded file * @return mixed successfully returned the uploaded file name, failed to return F Alse*/ public functionUp$file){ //determine if a file is uploaded via HTTP POST to prevent malicious spoofing /*if (! is_uploaded_file ($file [' tmp_name '])) {$this->errno = 5; Setting the error message number to 5 indicates that the illegal upload return is false; } */ //determine if the browser is successfully uploaded to the server side if($file[' error '] = = 0) { #Upload to Temp folder successfully, process/upload type judgment for temp file if(!In_array($file[' type '],$this-Mime)) { #type is not correct $this->errno =-1; return false; } //Determine file size if($file[' size '] >$this-Max_size) { #upload limits in size beyond the configuration file $this->errno =-2; return false; } //get the directory where the uploaded files are stored $sub _path=Date(' YMD '). ' /‘; if(!Is_dir($this->path.$sub _path)) { #the directory does not exist, creating the mkdir($this->path.$sub _path); } //file rename, by current date + random number + suffix name $file _name=Date(' Ymdhis ').uniqid().STRRCHR($file[' name '], '. '); //ready to start Uploading. if(Move_uploaded_file($file[' Tmp_name '],$this->path.$sub _path.$file _name)) { #Mobile Success return $sub _path.$file _name; } Else { #Move failed $this->errno =-3; return false; } } Else { #failed to upload to temp folder, set error number according to its error number $this->errno =$file[' error ']; return false; } } /** * Multiple File Upload method * @access public * @param $file array containing the uploaded file information is a two-dimensional array * @return Array successfully returned the uploaded filename , and if There's a failure, it's not too good to Handle.*/ public functionMultiup ($files){ //in multi-file upload, upload file information is also a multidimensional array, such as $_files[' userfile ' [' name '][0],$_files[' userfile '] [' name '][1]//we just need to traverse the array, Get the information for each uploaded file, call the Up method in turn foreach($files[' name '] as $key=$value) { #code ... $file[' name '] =$files[' name '] [$key]; $file[' type '] =$files[' type '] [$key]; $file[' Tmp_name '] =$files[' Tmp_name '] [$key]; $file[' Error '] =$files[' Error '] [$key]; $file[' size '] =$files[' size '] [$key]; //call the up method to complete the upload $filename[] =$this->up ($file); } return $filename; } /** * Get the error message and get the appropriate error prompt according to the error number * @access public * @return String returns an error message*/ public functionerror () {Switch($this-Errno) { case-1:return' Please check your file type, currently supported type has '.implode(‘,‘,$this-mime); break; case-2:return' The file exceeds the size specified by the system and cannot exceed the maximum '.$this-max_size; break; case-3:return' File move failed '; break; case1:return' The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini, and its size is '.Ini_get(' upload_max_filesize '); break; case2:return' The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form, and its size is '.$_post[' max_file_size ']; break; case3:return' files are only partially uploaded '; break; case4:return' No files were uploaded '; break; case5:return' Illegal upload '; break; case6:return' Unable to find temp folder '; break; case7:return' File Write Temp folder failed '; break; default:return' Unknown error, Supernatural event '; break; } }}/*Test code: Single File upload <form method= ' POST ' action= ' upload.php ' enctype= ' multipart/form-data ' > <input type = ' hidden ' n Ame= ' max_file_size ' value = ' 2000000 '/> <input type = ' FILE ' name = ' picture '/> <input type = ' Submit ' Value = ' upload '/></form> $upload = new Upload;if ($file _name = $upload->up ($_files[' picture ')) {echo ' upload succeeded, file Named ', $file _name;} else {echo ' upload failed with error message: ', $upload->error ();}*//*test code: multi-file upload <form action= "test.php" method= "POST" enctype= "multipart/form-data" > <label for= "upload picture" > </label> <input type= "file" name= ' logos[] '/> <br/> <label for= ' upload picture ' ></label> <input type= "file" name= ' logos[] '/> <br/> <label for= ' upload picture ' ></label> <input type= ' file ' name= ' logos [] '/> <br/> <input type= ' Submit ' name= ' OK '/></form> $upload = new upload (); $filename = $upload-& Gt;multiup ($_files[' Logos '); echo ' <pre> '; Var_dump ($filename); Echo ' <pre> '; */
Upload.class.php