pHP / * @ (#) UploadFile.php (beta) 2005/2/19 * * Exblog upload attachment class, can handle multiple upload files in both users. The validity file is used to store to the specified directory. * You can return to the relevant useful information of the upload file for other programs. (Such as file name, type, size, save path) * Please see the bottom of this class (UPLOADFILE class usage) information. * * This program is a free program, the source code user can be arbitrarily changed (do not use for commercial purposes), but please keep this copyright information! * * @Author hexuzhong
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
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 ("
/ 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
" ;}} * /?>