PHPEncapsulates the multi-file upload class instance and usage in detail, the example describes
PHPEncapsulates a multi-file upload class instance and usage. Share to everyone for reference study
PHP, specifically as follows:
<?php/**//* * @ (#) uploadfile.php * * Can process the user multiple upload files simultaneously. The validated file is saved to the specified directory after validation. * You can return useful information about uploading files for use by other programs. (such as file name, type, size, save path) * Use method see the bottom of this class (UploadFile class use note) information. * */class UploadFile {var $user _post_file = Array ();//user uploaded file Var $save _file_path; The path of storing the user upload file var $max _file_size; Maximum file size var $last _error; Record last error message//default allows user to upload file type var $allow _type = array (' gif ', ' jpg ', ' png ', ' zip ', ' rar ', ' txt ', ' Doc ', ' pdf '); var $final _file_path; The final saved file name var $save _info = Array (); Returns a set of useful information that is used to prompt the user. /**//** * constructor, with initialization related information, user to upload files, storage path, etc. * @param Array $file user uploaded file * @param String $path Store the path of user uploaded file * @param Integer $ Size allows the user to upload a file (in bytes) * @param array $type This array holds the file type uploaded by the user of the allowable meter */function UploadFile ($file, $path, $size = 2097152, $type = {$this->user_post_file = $file; $this->save_file_path = $path; $this->max_file_size = $size;//If the user does not fill in the file size, The default is 2M. if ($type! = ") $this->allow_type = $type; /**//** * Store users upload files, verify the legality passed, save to the specified location. * @access public * @return int value is 0 o'clock upload failed, non 0 indicates the number of successful uploads. */function upload () {for ($i = 0; $i < count ($this->user_post_file[' name '); $i + +) {//If the current file upload function, perform the next step. if ($this->user_post_file[' error '] [$i] = = 0) {//Take the current file name, temporary file name, size, extension, which will be used later. $name = $this->user_post_file[' name ' [$i]; $tmpname = $this->user_post_file[' tmp_name ' [$i]; $size = $this->user_post_file[' size ' [$i]; $mime _type = $this->user_post_file[' type ' [$i]; $type = $this->getfileext ($this->user_post_file[' name '] [$i]); Detects whether the current upload file size is legitimate. if (! $this->checksize ($size)) {$this->last_error = "The file size is too big. File name is: ". $name; $this->halt ($this->last_error); Continue }//detects if the current upload file name extension is valid. if (! $this->checktype ($type)) {$this->last_error = "unallowable file type:.". $type. " File name is: ". $name; $this->halt ($this->last_error); Continue }//detects if the current upload file is being submitted illegally. if (!is_uploaded_file ($tmpname)) {$this->last_error = "Invalid Post file method. File name is: ". $name; $this->halt ($this->lAST_ERROR); Continue }//After moving the file, rename the file with. $basename = $this->getbasename ($name, ".". $type); The file name after the move $saveas = $basename. " -". Time ().". $type; Save the new file name to the specified directory, format: Store path + file name + time + extension $this->final_file_path = $this->save_file_path. " /". $saveas; if (!move_uploaded_file ($tmpname, $this->final_file_path)) {$this->last_error = $this->user_post_file[' Error '] [$i]; $this->halt ($this->last_error); Continue }//stores information about the current file for other programs to call. $this->save_info[] = Array ("name" = = $name, "type" = = $type, "mime_type" = = $mime _type, "si Ze "= $size," saveas "= $saveas," path "= $this->final_file_path); }} return count ($this->save_info); Returns the number of files successfully uploaded}/**//** * Return some useful information for use elsewhere. * @access public * @return Array returns the final saved path */function Getsaveinfo () {return $this->save_info;}/**//** * Detects user commit file size is No legal * @param Integer $size The size of the uploaded file by the user * @access Private * @return Boolean if true to indicate the size is legal, or vice versa * * function Checksize ($size) {if ($size > $this->max_file_size) {return false;} else {return true;}}/**//** * Detect user Submissions The type of the piece is legal * @access Private * @return Boolean if true to indicate that the type is legal and vice versa */function Checktype ($extension) {foreach ($this->all Ow_type as $type) {if (strcasecmp ($extension, $type) = = 0) return true;} return false; /**//** * Display error message * @param $msg error message to display * @access private */function Halt ($msg) {printf ("<b><uploadfile Erro R:></b>%s <br>\n ", $msg); }/**//** * Fetch file extension * @param string $filename given file name to take extension * @access private * @return String returns the given file extension */function GetFile EXT ($filename) {$stuff = PathInfo ($filename); return $stuff [' extension '];}/**//** * takes the given file file name, excluding the extension. * Eg:getbasename ("j:/hexuzhong.jpg"); Returns Hexuzhong * * @param string $filename given file name to be taken * @access private * @return String returns the file name */function Getbasename ($fi Lename, $type) {$basename = basename ($filename, $type); return $basename;}} /**//******************** UploadFileClass using comments//note, upload component The Name property is used in the array form, whether one or more, such as: <input type= "file" Name= "user_upload_file[]" ><input type= "file" Name= "user_upload_file[]" >//if the user clicked the Upload button. if ($_post[' action '] = = "Upload") {//Set the type of file that the user is allowed to upload. $type = Array (' gif ', ' jpg ', ' png ', ' zip ', ' rar '); Instantiate the upload class, the first parameter is the file group that the user uploads, the second parameter is the storage path,//The third parameter is the maximum size of a file. If not, the default is 2M//The fourth parameter is an array of types that are allowed to be uploaded by a user. If not, the default is GIF, JPG, PNG, zip, rar, txt, doc, pdf $upload = new UploadFile ($_files[' user_upload_file '), ' j:/tmp ', 100000, $ty PE); Upload the user file, return an int value, for the number of uploaded files successfully. $num = $upload->upload (); if ($num! = 0) {echo "Upload successful <br>";//Get information about the file, file name, type, size, path. Print it out with Print_r (). Print_r ($upload->getsaveinfo ()); Format: Array//(//[0] = = Array (//[Name] = example.txt//[Type] = TXT//[size] = 526/// [Path] = j:/tmp/example-1108898806.txt//)//) echo $num. " File upload Success "; } else {echo "Upload failed <br>";}} */?>