zoukankan      html  css  js  c++  java
  • 多文件上传类

    [代码] [PHP]代码
    view source

    01    <?php
    02      define('ROOT','D:/Program Files/www/test/');
    03      class Files_Tool{
    04         protected static $allowExt=array('.jpg','.jpeg','.png','.gif','.bmp','.svg','.chm','.pdf','.zip','.rar','.tar','.gz','.bzip2','.ppt','.doc');
    05         public      static $wrong=array();
    06         public      static $path=array();
    07         protected static $error=array(
    08                                         0=>'文件上传失败,没有错误发生,文件上传成功',
    09                                         1=>'文件上传失败,上传的文件超过了 php.ini中upload_max_filesize 选项限制的值',
    10                                         2=>'文件上传失败,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值',
    11                                         3=>'文件上传失败,文件只有部分被上传',
    12                                         4=>'文件上传失败,没有文件被上传',
    13                                         5=>'文件上传失败,未允许的后缀',
    14                                         6=>'文件上传失败,找不到临时文件夹.PHP 4.3.10 和 PHP 5.0.3 引进',
    15                                         7=>'文件上传失败,文件写入失败.PHP 5.1.0 引进',
    16                                         8=>'文件上传失败,未接收到表单域的NAME',
    17                                         9=>'文件上传失败,,错误未知'
    18                                       );
    19    
    20              public static function upload($name){
    21                 //检测是否接收到表单域的NAME
    22                 if(!isset($_FILES[$name])){
    23                    self::$wrong[]=8;
    24                    return false;
    25                 }
    26                //3维数组简化成2维数组
    27                $files=array_shift($_FILES);
    28                //获取后缀
    29                $files=self::get_Ext($files);
    30                //处理文件次数
    31                $n=count($files['name']);
    32                for($i=0;$i<$n;$i++){
    33                   //查看当前文件是否有错误信息,有则跳过当前文件,处理下个文件
    34                   if($files['error'][$i]!=0){                               
    35                     self::$wrong[$i+1]=$files['error'][$i];
    36                     continue;
    37                   }
    38                   //查看当前文件的后缀,是否允许,如果不允许,跳过当前文件
    39                   if(!in_array($files['name'][$i],self::$allowExt)){
    40                       self::$wrong[$i+1]=5;
    41                       continue;
    42                   }
    43                   //路径
    44                   $dir=self::time_Dir();
    45                   //文件名
    46                   $name=self::rand_Name();
    47                   //后缀
    48                   $ext=$files['name'][$i];
    49                   //文件位置
    50                   $path=$dir.$name.$ext;
    51                   //移动临时文件,如果失败,跳过当前文件
    52                   if(!move_uploaded_file($files['tmp_name'][$i],$path)){
    53                     self::$wrong[$i]=9;
    54                     continue;
    55                   }
    56                   //存入路径
    57                   self::$path[$i+1]=strtr($path,array(ROOT=>''));
    58                    
    59                }
    60                return self::$path;
    61              }
    62                     
    63             //获取后缀的方法
    64             protected static function get_Ext($arr){
    65                   if(!is_array($arr) || !isset($arr['name'])){return false;}
    66                   foreach($arr['name'] as $k=>$v){
    67                     $arr['name'][$k]=strtolower(strrchr($v,'.'));
    68                   }
    69                   return $arr;
    70             }
    71             //以日期生成路径
    72             protected static function time_Dir(){
    73                 $dir=ROOT.'Data/images/'.date('Y/m/d/',time());
    74                 if(!is_dir($dir)){
    75                    mkdir($dir,0777,true);
    76                 }
    77                 return $dir;
    78             }
    79             //生成随机文件名
    80              protected static function rand_Name(){
    81                 $str=str_shuffle('1234567890qwertyuiopasdfghjklzxcvbnm');
    82                 $str=substr($str,0,6);
    83                 return $str;
    84              }
    85            //错误接口
    86            public static function errors(){
    87               foreach(self::$wrong as $k=>$v){
    88                    self::$wrong[$k]='第'.$k.'个'.self::$error[$k];
    89              }
    90              return self::$wrong;
    91            }
    92    
    93      }

  • 相关阅读:
    洛谷 P1194 飞扬的小鸟 题解
    洛谷 P1197 星球大战 题解
    洛谷 P1879 玉米田Corn Fields 题解
    洛谷 P2796 Facer的程序 题解
    洛谷 P2398 GCD SUM 题解
    洛谷 P2051 中国象棋 题解
    洛谷 P1472 奶牛家谱 Cow Pedigrees 题解
    洛谷 P1004 方格取数 题解
    洛谷 P2331 最大子矩阵 题解
    洛谷 P1073 最优贸易 题解
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2891541.html
Copyright © 2011-2022 走看看