PHP multi-File Upload instance,
This paper describes the method of uploading multiple files in PHP. Share to everyone for your reference. Specific as follows:
<?phpdefine (' ROOT ', ' D:/program files/www/test/'); class files_tool{protected static $allowExt =array ('. jpg ', '. JPEG ', '. png ', '. gif ', '. bmp ', '. svg ', '. chm ', '. pdf ', '. zip ', '. rar ', '. Tar ', '. Gz ', '. bzip2 ', '. ppt ', '. Doc '; public static $wrong =array (); public static $path =array (); protected static $error =array (0=> ' file upload failed, no error occurred, file upload succeeded ', 1=> ' file upload failed, uploaded files more than php.ini in Upload_max_filesize Option limit value ', 2=> ' file upload failed, upload file size exceeds the value specified in HTML form max_file_size option ', 3=> ' file upload failed, file only partially uploaded ', 4=> ' file upload failed, no file was uploaded ', 5=> ' File upload failed, not allowed suffix ', 6=> ' file upload failed, unable to find temp folder. PHP 4.3.10 and PHP 5.0.3 introduced ', 7=> ' file upload failed, file write failed. PHP 5.1.0 introduced ', 8=> ' file upload failed, did not receive the form field name ', 9=> ' file upload failed, error unknown '); public static function upload ($name) {//detects whether to receive the form field's name if (!isset ($_files[$name])) {self:: $wrong []=8; return false; }//3-d arrays are simplified into 2-dimensional arrays $files =array_shift ($_files); Get suffix $files =self::get_ext ($files); Number of files processed $n =count ($files [' name ']); for ($i =0; $i < $n; $i + +) {//view the current file for an error message, or skip the current file,The next file if ($files [' Error '] [$i]!=0) {self:: $wrong [$i +1]= $files [' Error '] [$i]; Continue }//View the current file's suffix, allow, if not allowed, 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 }//Deposit path Self:: $path [$i +1]=strtr ($path, Array (root=> ")); } return Self:: $path; }//Get suffix method 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; }//Date generation 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 randomFile name protected static function Rand_name () {$str =str_shuffle (' 1234567890qwertyuiopasdfghjklzxcvbnm '); $str =substr ($str, 0,6); return $str; }//Error interface public static function errors () {foreach (self:: $wrong as $k + = $v) {self:: $wrong [$k]= '. ' $k. ' ". Self:: $er ror[$k]; } return Self:: $wrong; }}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1029589.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029589.html techarticle PHP Multi-File Upload instance, this article describes the PHP multi-File upload method. Share to everyone for your reference. Specific as follows: Phpdefine (' ROOT ', ' D:/program files/www/test/'), class ...