Multiple 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','.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 was successful',
- 1=>'File upload failed, the uploaded file exceeded the value limited by the upload_max_filesize option 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, only part of the file was uploaded',
- 4=>'File upload failed, no file was uploaded',
- 5=>'File upload failed, suffix not allowed',
- 6=>'File upload failed, temporary folder not found. Introduced in PHP 4.3.10 and PHP 5.0.3',
- 7=>' File upload failed, file writing failed. Introduced in PHP 5.1.0',
- 8=>'File upload failed, the name of the form field was not received',
- 9=>'File upload failed,,error unknown'
- );
-
- public static function upload($name){
- //Check whether the NAME of the form field is received
- if(!isset($_FILES[$name])){
- self::$wrong[]=8;
- return false;
- }
- //3D array is simplified into 2D array
- $files=array_shift($_FILES);
- //Get the suffix
- $files=self::get_Ext($files);
- //Process files Number of times
- $n=count($files['name']);
- for($i=0;$i<$n;$i++){
- //Check whether the current file has an error message, if so, skip the current one file, process the next file
- if($files['error'][$i]!=0){
- self::$wrong[$i+1]=$files['error'][$i];
- continue;
- }
- //Check the suffix of the current file, whether it is allowed, 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 temporary files, if failed, Skip the current file
- if(!move_uploaded_file($files['tmp_name'][$i],$path)){
- self::$wrong[$i]=9;
- continue;
- }
- //Save Path
- self::$path[$i+1]=strtr($path,array(ROOT=>''));
-
- }
- return self::$path;
- }
-
- //Get the 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;
- }
- //Generate by date 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,0,6);
- return $str;
- }
- //Error interface
- public static function errors(){
- foreach(self::$wrong as $k=>$v){
- self: :$wrong[$k]='th'.$k.'th'.self::$error[$k];
- }
- return self::$wrong;
- }
-
- }
Copy code
|