This article mainly introduces PHP-encapsulated multi-file Upload instances and usage, and analyzes in detail the definitions and usage of php multi-file Upload classes in combination with the instance form, for more information about PHP-encapsulated multifile Upload instances and usage, see the next article. the definitions and usage of php multifile Upload classes are analyzed in detail based on the instance format, for more information, see
This example describes the PHP-encapsulated multi-file Upload instance and usage. We will share this with you for your reference. The details are as follows:
User_post_file = $ file; $ this-> save_file_path = $ path; $ this-> max_file_size = $ size; // If you do not specify the file size, the default value is 2 MB. if ($ type! = '') $ This-> allow_type = $ type;}/** // ** stores the uploaded files, verifies validity, and stores the files in the specified location. * @ Access public * @ return int if the value is 0, the upload fails. a value other than 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 is enabled, perform the next step. If ($ this-> user_post_file ['error'] [$ I] = 0) {// Obtain the current file name, temporary file name, size, and 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]); // checks whether the size of the currently uploaded file is valid. If (! $ This-> checkSize ($ size) {$ this-> last_error = "The file size is too big. file name is :". $ name; $ this-> halt ($ this-> last_error); continue;} // checks whether the current file extension is valid. If (! $ This-> checkType ($ type) {$ this-> last_error = "Unallowable file type :. ". $ type. "File name is :". $ name; $ this-> halt ($ this-> last_error); continue;} // checks whether the uploaded file is 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 for use. $ Basename = $ this-> getBaseName ($ name ,". ". $ type); // The moved file name $ saveas = $ basename. "-". time (). ". ". $ type; // combine the new file name and save it 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-> 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, "size" => $ size, "saveas" => $ saveas, "path" => $ this-> final_file_path) ;}} return count ($ this-> save_info ); // return the number of successfully uploaded files}/*** // *** return some useful information for use elsewhere. * @ Access public * @ return Array returns the final saved path */function getSaveInfo () {return $ this-> save_info ;} /** // *** check whether the size of the file submitted by the user is legal * @ param Integer $ size the size of the file uploaded by the user * @ access private * @ return boolean if true, the file size is legal, otherwise it is invalid */function checkSize ($ size) {if ($ size> $ this-> max_file_size) {return false;} else {return true ;}} /** // *** check whether the file type submitted by the user is legal * @ access private * @ return boolean if it is true, the type is legal, otherwise it is invalid */function checkType ($ extension) {foreach ($ this-> allow_type as $ type) {if (strcasecmp ($ extension, $ type) = 0) return true;} return false ;} /** // ** display error information * @ param $ msg the error message to be displayed * @ access private */function halt ($ msg) {printf ("
% S
\ N ", $ msg );} /** // *** get the file extension * @ param String $ filename the given file to get the extension * @ access private * @ return String returns the given file extension */function getFileExt ($ filename) {$ stuff = pathinfo ($ filename); return $ stuff ['extension'];}/** // *** get the given file name, excluding the extension. * Eg: getBaseName ("j:/hexuzhong.jpg "); // return hexuzhong ** @ param String $ filename the file to be retrieved * @ access private * @ return String returns the file name */function getBaseName ($ filename, $ type) {$ basename = basename ($ filename, $ type); return $ basename ;}} /*** // *********************** use comments for the UploadFile class // note, the name attribute of the uploaded component must be in the array format, for example:// If the user clicks the upload button. If ($ _ POST ['action'] = "upload") {// sets the file type that allows users to upload. $ Type = array ('GIF', 'jpg ', 'PNG', 'Zip ', 'rar'); // instantiate the upload class, the first parameter is the file group uploaded by the user, the second parameter is the storage path, and the third parameter is the maximum file size. If this parameter is not specified, the default value is 2 M. // The fourth parameter is an array of user-uploaded types. If this parameter is left blank, the default value is gif, jpg, png, zip, rar, txt, doc, pdf $ upload = new UploadFile ($ _ FILES ['User _ upload_file '], 'J: /tmp ', 100000, $ type); // upload user files. The int value is returned, indicating the number of successfully uploaded files. $ Num = $ upload-> upload (); if ($ num! = 0) {echo "uploaded successfully
"; // Obtain information about the file, including the file name, type, size, and 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. "files uploaded successfully";} else {echo "Upload failed
";}}*/?>