PHP multi-file upload class

xiaoxiao2021-03-06  14

* @version beta, 2005 / 2/19 * @since exblog-1.2.1 * * @BTW: Does the parviation style change? Note accounts for about 60% -70% ^ ___ ^ * / class uploadFile {var $ user_post_file = array (); // User upload file var $ save_file_path; // Store user upload path VAR $ max_file_size; // file Maximum size var $ last_error; // Record the last error message // By default allows users to upload file type var $ allow_type = array ('gif ",' jpg ',' png ',' zip ',' rar ',' txt ',' DOC ',' PDF '); VAR $ final_file_path; // Final Saved file name

VAR $ save_info = array (); // Returns a group of useful information for prompting users. / ** * Constructor, with initialization related information, users to upload files, storage paths, etc. * @Param Array $ file users uploaded file * @Param string $ PATH Store user uploaded file * @Param Integer $ size Allow users to upload file size (bytes) * @Param Array $ TYPE stores in this array * / 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 user upload files, after the legality is passed, store it to the specified location. * @Access public * @Return INT value is 0, and the number of non-0 means upload success. * / Function upload () {for ($ I = 0; $ I user_post_file ['name'); $ i ) {// If the current file upload function, the next step is performed. IF ($ this-> user_post_file ['error'] [$ I] == 0) {// Take the current file name, temporary file name, size, extension, 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]); // Detection Whether the current upload file size is legal. IF ($ this-> Checksize) {$ this-> last_error = "The File size is:". $ name; $ this-> halt ($ this-> last_error); CONTINUE;} // Detect whether the current upload file extension is legal. IF (! $ THIS-> CheckType) {$ this-> last_error = "Unallowable file type:." $ TYPE. "File Name is:". $ name; $ this-> halt ($ this- > LAST_ERROR); Continue;} // Detects whether the current upload file is illegally submitted.

IF ($ THIS-> LAST_ERROR = "INVALID POST FILE Method. File Name IS:". $ name; $ this-> halt ($ this-> last_error); Continue;} // Mobile After the file, rename the file. $ base = $ this-> getBasename ($ name, ".". $ type); // Moved file name $ saveas = $ basename. "-". Time (). "." $ type; // The new file name is saved to the specified directory, format: storage path file name time extension $ this-> final_file_path = $ this-> save_file_path. "/". $ Saveas; if (! Move_uploaded_file ($ TMPNAME, $ this-> firm_file_path)) {$ this-> last_error = $ this-> user_post_file ['error'] [$ I]; $ this-> halt ($ this-> last_error); Continue;} // Store the current file Related information so that other programs are called. $ this-> save_info [] = array ("name" => $ name, "type" => $ type, "mime_type" => $ mime_type, "size" => $ size, "saveas" => $ saveas, "Path" => $ this-> final_file_path);}}}}}}}}}} Return Count ($ this-> save_info); // Returns the number of uploaded successful files} / ** * Returns some useful information for other places. * @access public * @Return Array Returns the final saved path * / function getsaveinfo () {return $ this-> save_info;} / ** * Detect the user to submit the file size ration * @Param Integer $ SIZE user upload file size * @access private * @Return Boolean If you tell the size of the size, it means that the size is legal.

/ ** * Detect whether the user submission file type is legal * @access private * @Return Boolean If you explain the type of type, it is not legal * / function checktype ($ extension) {foreach ($ this-> allow_type as $ type) { IF (STRCASECMP ($ EXTENSION, $ TYPE) == 0) Return true;} Return False;} / ** * Display error message * @param $ msg To display error message * @access private * / function halt ($ msg ) {Printf (" % s
/ n", $ msg);} / ** * Take a file extension * @Param string $ filename gives an extension Name file * @access private * @Return string Returns a given file extension * / function getfileext ($ filename) {$ stuff = pathinfo ($ filename); return $ stuff ['extension'];} / ** * A given file file name does not include the extension. * EG: getBasename ("j: / /hexuzhong.jpg"); // Return Hexuzhong * * @Param string $ filename gives file name file * @access private * @return string Return to file name * / function getBasename ( $ FileName, $ TYPE) {$ BaseName = BaseName ($ filename, $ type); returnome;}} / ****************************************************************** UPLOADFILE // Note that the upload component Name attribute does not care if you still have multiple forms, such as

// If the user clicks the upload button. IF ($ _POST ['Action'] == "Upload") {// Settings Allow users to upload file types. $ TY = Array ('GIF', 'JPG', 'PNG', 'Zip', 'Rar'); // Instantiation Upload Class, the first parameter is the file group uploaded by the user, the second parameter is stored Path, // The third parameter is the maximum size of the file. If the default does not fill the default 2m // fourth parameter is an array of types uploaded by users. If you don't fill in default, JPG, PNG, ZIP, RAR, TXT, DOC, PDF $ UPLOAD = New UploadFile ($ _ files ['user_upload_file'], 'J: / TMP', 100000, $ TYPE); // Upload the user file, return the int value, the number of successful files to upload. $ Num = $ UPLOAD-> UPLOAD (); if ($ Num! = 0) {Echo "Upload Success
"; // Take information about information, file name, type, size, path. Print out with Print_R (). Print_r ($ upload-> getSaveInfo ()); // format is: array // (// [0] => array (// [name] => eXample.txt // [type] => txt // [ Size] => 526 // [PATH] => J: /TMP/EXAMPLE-1108898806.txt //) //) ECHO $ ​​NUM. "One file upload success";} else {echo "upload failed
" ;}} * /?>

转载请注明原文地址:https://www.9cbs.com/read-48277.html

New Post(0)