PHP implements Multifile upload,
This example describes how to upload multiple files in PHP. Share it with you for your reference. The specific implementation method is as follows:
<? Phpdefine ('root', 'd:/Program Files/www/test/'); class Files_Tool {protected static comment'); public static $ wrong = array (); public static $ path = array (); protected static $ error = array (0 => 'file Upload Failed, no error occurred, File Uploaded successfully ', 1 => 'file Upload Failed. the uploaded file exceeds php. value restricted by the upload_max_filesize option in ini ', 2 =>' File Upload Failed. The size of the uploaded file exceeds The value specified by the MAX_FILE_SIZE option in the HTML form ', 3 =>' File Upload Failed, only part of the file was uploaded ', 4 =>' File Upload Failed, no file was uploaded ', 5 => 'file Upload Failed, not allowed suffixes ', 6 =>' file upload failed, and the Temporary Folder cannot be found. PHP 4.3.10 and PHP 5.0.3 import ', 7 =>' File Upload Failed, file write failed. PHP 5.1.0 introduces ', 8 =>' File Upload Failed, the NAME of the form field is not received ', 9 => 'file Upload Failed, error unknown '); public static function upload ($ name) {// checks whether the NAME if (! Isset ($ _ FILES [$ name]) {self: $ wrong [] = 8; return false ;} // The 3-dimensional array is simplified to a 2-dimensional array $ files = array_shift ($ _ FILES); // get the suffix $ files = self: get_Ext ($ files ); // number of file processing times $ n = count ($ files ['name']); for ($ I = 0; $ I <$ n; $ I ++) {// check whether the current file has an error message. if yes, skip the current file and process the next file if ($ files ['error'] [$ I]! = 0) {self ::$ wrong [$ I + 1] = $ files ['error'] [$ I]; continue;} // view the suffix of the current file, allow or not. if not, skip the current file if (! In_array ($ files ['name'] [$ I], self ::$ allowExt) {self ::$ wrong [$ I + 1] = 5; continue ;} // path $ dir = self: time_Dir (); // file name $ name = self: rand_Name (); // suffix $ ext = $ files ['name'] [$ I]; // File Location $ path = $ dir. $ name. $ ext; // move the temporary file. if it fails, skip the current file if (! Move_uploaded_file ($ files ['tmp _ name'] [$ I], $ path) {self ::$ wrong [$ I] = 9; continue ;} // storage path self: $ path [$ I + 1] = strtr ($ path, array (ROOT => '');} return self: $ path ;} // method for obtaining the suffix protected static function get_Ext ($ arr) {if (! Is_array ($ arr) |! Isset ($ arr ['name']) {return false;} foreach ($ arr ['name'] as $ k => $ v) {$ arr ['name'] [$ k] = strtolower (strrchr ($ v ,'. ');} return $ arr;} // generate the path protected static function time_Dir () {$ dir = ROOT. 'Data/images /'. date ('y/m/d/', time (); if (! Is_dir ($ dir) {mkdir ($ dir, 0777, true);} return $ dir;} // generate a random file name protected static function rand_Name () {$ str = str_shuffle ('1234567890qwertyuiopasdfghjklzxcvbnm '); $ str = substr ($ str,); return $ str ;}// error interface public static function errors () {foreach (self ::$ wrong as $ k =>$ v) {self ::$ wrong [$ k] = 'di '. $ k. 'tags '. self: $ error [$ k];} return self: $ wrong ;}}
I hope this article will help you with php programming.