Multi-File upload-PHP
- Define (' ROOT ', ' D:/program files/www/test/');
- Class files_tool{
- protected static $allowExt =array ('. jpg ', '. jpeg ', '. png ', '. gif ', '. bmp ', '. svg ', '. chm ', '. pdf ', '. zip ', '. rar ', '. . 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, the uploaded file exceeds the value of upload_max_filesize option limit in php.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, file is only partially uploaded ',
- 4=> ' File upload failed, no files were uploaded ',
- 5=> ' File upload failed, not allowed suffix ',
- 6=> ' File upload failed and the Temp folder could not be found. 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 if the name of the form field is received
- if (!isset ($_files[$name])) {
- Self:: $wrong []=8;
- return false;
- }
- 3-D arrays are simplified into 2-d 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 + +) {
- See if the current file has an error message, skip the current file, and process 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 ();
- Filename
- $name =self::rand_name ();
- Suffix
- $ext = $files [' name '] [$i];
- File location
- $path = $dir. $name. $ext;
- Move temporary file, if failed, skip 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;
- }
- Ways to get suffixes
- 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 a path with a date
- 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 Random file names
- 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:: $error [$k];
- }
- Return self:: $wrong;
- }
- }
Copy Code |