Discuz教程网

简单的文件上传类

[复制链接]
authicon 09927306 发表于 2011-1-12 17:00:16 | 显示全部楼层 |阅读模式

  1. <?php   
  2. /**  
  3. *   FileUplaod class  
  4. *   部分代码源自网络  
  5. *   2010-3-23  
  6. */  
  7. class FileUpload {   
  8.     // 验证上传文件是否为规定大小   
  9.     private static function size($_formName = \'UploadFile\', $size = \'10M\') {   
  10.         $files = self::dealFiles($_FILES[$_formName]);   
  11.         $return = TRUE;   
  12.         foreach($files as $file){   
  13.             if ((int) $file[\'error\'] !== UPLOAD_ERR_OK){   
  14.                 return TRUE;   
  15.             }   
  16.             $size = strtoupper($size);   
  17.             if ( ! preg_match(\'/[0-9]++[BKMG]/\', $size)){   
  18.                 return FALSE;   
  19.             }   
  20.             switch (substr($size, -1)){   
  21.                 case \'G\': $size = intval($size) * pow(1024, 3); break;   
  22.                 case \'M\': $size = intval($size) * pow(1024, 2); break;   
  23.                 case \'K\': $size = intval($size) * pow(1024, 1); break;   
  24.                 default:  $size = intval($size);                break;   
  25.             }   
  26.             $return = ($return and ($file[\'size\'] <= $size));   
  27.         }   
  28.         return $return;   
  29.     }   
  30.     /**  
  31.     * 验证上传文件后缀 默认为图片类型  
  32.     * _formName string   default UploadFile 表单元素name值  
  33.     * allowExt  array|string 允许的后缀类型 可以为数组或者字符串  
  34.     * return    bool         是否验证成功  
  35.     */  
  36.     public static function valid($_formName = \'UploadFile\', $_allowExt = array(\'jpeg\',\'gif\',\'png\',\'jpg\',\'bmp\'), $size = \'10M\') {   
  37.         if(! self::size($_formName,$size)){   
  38.             return false;   
  39.         }   
  40.         $return = TRUE;   
  41.         if(is_array($_FILES[$_formName][\'name\'])){   
  42.             foreach($_FILES[$_formName][\'name\'] as $_name){   
  43.                 $_ext = pathinfo($_name,PATHINFO_EXTENSION);   
  44.                 $return = ($return AND in_array($_ext, $_allowExt));   
  45.             }   
  46.             return $return;   
  47.         }else{   
  48.             $_ext = pathinfo($_FILE[$_formName],PATHINFO_EXTENSION);   
  49.             if(in_array($_ext, $_allowExt)){   
  50.                  return TRUE;   
  51.             }   
  52.             return FALSE;   
  53.         }   
  54.         return FALSE;   
  55.     }   
  56.     /**  
  57.     * 保存上传文件  
  58.     * _formName string  default UploadFile 表单元素name值  
  59.     * _savePath string  default NULL       文件保存路径(默认当前路径./upload/目录)  
  60.     * _fileName string  default NULL       文件保存名称 默认时间戳+原文件名  
  61.     * _chmode   int     default 0664       上传后的文件权限  
  62.     */  
  63.     public static function save($_formName = \'UploadFile\', $_savePath = NULL, $_fileName = NULL, $_chmode = 0664) {   
  64.         $file = \'\';   
  65.         $files = array();   
  66.         if(is_array($_FILES[$_formName][\'name\'])){   
  67.             $_fileName = \'\';   
  68.             $files = self::dealFiles($_FILES[$_formName]);   
  69.             $fileNames = array();   
  70.             foreach($files as $file){   
  71.                 $fileNames[] = self::upload($file,$_savePath,$_fileName, $_chmode);   
  72.             }   
  73.             return $fileNames;   
  74.         }else{   
  75.             $file = $_FILES[$_formName];   
  76.             $fileName = self::upload($file,$_savePath,$_fileName, $_chmode);   
  77.         }   
  78.         return $fileName;   
  79.     }   
  80.     /**  
  81.     * 保存上传文件  
  82.     * 参数同save()  
  83.     */  
  84.     private static function upload($file = array(), $_savePath = NULL, $_fileName = NULL, $_chmode = 0664) {   
  85.         if(emptyempty($file)){ return FALSE;}   
  86.         if($_fileName == NULL){   
  87.             $_fileName = time().rand(1,100).$file[\'name\'];   
  88.         }   
  89.         $_fileName = preg_replace(\'/\\s+/\', \'_\', $_fileName);   
  90.         if ($_savePath == NULL){   
  91.             $_savePath = \'./upload/\';   
  92.         }   
  93.         self::mkdirs($_savePath,0777);   
  94.         $_savePath = rtrim($_savePath, \'/\').\'/\';   
  95.         if ( ! is_writable($_savePath)){   
  96.             throw new Exception(\'The directory \'.$_savePath.\' is not_writable !\');   
  97.         }   
  98.         if (is_uploaded_file($file[\'tmp_name\']) AND move_uploaded_file($file[\'tmp_name\'], $filename = $_savePath.$_fileName)){   
  99.             if ($chmod !== FALSE){   
  100.                 chmod($filename, $_chmod);   
  101.             }   
  102.             return $filename;   
  103.         }else{   
  104.             throw new Exception(\'Move uploaded File Error!\'.$file[\'tmp_name\']);   
  105.         }   
  106.         return FALSE;   
  107.   
  108.     }   
  109.     // 创建目录树   
  110.     public static function mkdirs($dir, $mode = 0777) {   
  111.         if (!is_dir($dir)) {   
  112.             self::mkdirs(dirname($dir), $mode);   
  113.             return mkdir($dir, $mode);   
  114.         }   
  115.         return TRUE;   
  116.     }   
  117.     // 多文件上传 数组格式转换   
  118.     private static function dealFiles(&$files) {   
  119.        $fileArray = array();   
  120.        $count = count($files[\'name\']);   
  121.        $keys = array_keys($files);   
  122.        for ($i=0; $i<$count; $i++) {   
  123.            foreach ($keys as $key) {   
  124.                if(emptyempty($files[$key][$i])){   
  125.                    unset($fileArray[$i][$key]);   
  126.                    unset($fileArray[$i][\'error\']);   
  127.                }else{   
  128.                     $fileArray[$i][$key] = $files[$key][$i];   
  129.                }   
  130.            }   
  131.        }   
  132.        // 剔除为空的元素   
  133.        foreach($fileArray as $key=>$value){   
  134.             if(emptyempty($value)){   
  135.                 unset($fileArray[$key]);   
  136.             }   
  137.        }   
  138.        return $fileArray;   
  139.     }   
  140. }   
  141. // 测试   
  142. if( !emptyempty($_FILES[\'UploadFile\'])){   
  143.     $allowExt = array(\'jpg\');   
  144.     if(FileUpload::valid(\'UploadFile\', $allowExt)){   
  145.          $test = FileUpload::save(\'UploadFile\');   
  146.          print_r($test);   
  147.     }else{   
  148.         echo \'error\';   
  149.     }   
  150. }   
  151. ?>   
  152. <form action="#" method="post" enctype="multipart/form-data">   
  153. <input type="file" name="UploadFile[]"><br/>   
  154. <input type="file" name="UploadFile[]"><br/>   
  155. <input type="file" name="UploadFile[]"><br/>   
  156. <input type="file" name="UploadFile[]"><br/>   
  157. <input type="submit" value="sub">   
  158. </form>   
  159. <?php
  160. /**
  161. *   FileUplaod class
  162. *   部分代码源自网络
  163. *   2010-3-23
  164. */
  165. class FileUpload {
  166.     // 验证上传文件是否为规定大小
  167.     private static function size($_formName = \'UploadFile\', $size = \'10M\') {
  168.         $files = self::dealFiles($_FILES[$_formName]);
  169.         $return = TRUE;
  170.         foreach($files as $file){
  171.             if ((int) $file[\'error\'] !== UPLOAD_ERR_OK){
  172.                 return TRUE;
  173.             }
  174.             $size = strtoupper($size);
  175.             if ( ! preg_match(\'/[0-9]++[BKMG]/\', $size)){
  176.                 return FALSE;
  177.             }
  178.             switch (substr($size, -1)){
  179.                 case \'G\': $size = intval($size) * pow(1024, 3); break;
  180.                 case \'M\': $size = intval($size) * pow(1024, 2); break;
  181.                 case \'K\': $size = intval($size) * pow(1024, 1); break;
  182.                 default:  $size = intval($size);                break;
  183.             }
  184.             $return = ($return and ($file[\'size\'] <= $size));
  185.         }
  186.         return $return;
  187.     }
  188.     /**
  189.     * 验证上传文件后缀 默认为图片类型
  190.     * _formName string   default UploadFile 表单元素name值
  191.     * allowExt  array|string 允许的后缀类型 可以为数组或者字符串
  192.     * return    bool         是否验证成功
  193.     */
  194.     public static function valid($_formName = \'UploadFile\', $_allowExt = array(\'jpeg\',\'gif\',\'png\',\'jpg\',\'bmp\'), $size = \'10M\') {
  195.         if(! self::size($_formName,$size)){
  196.             return false;
  197.         }
  198.         $return = TRUE;
  199.         if(is_array($_FILES[$_formName][\'name\'])){
  200.             foreach($_FILES[$_formName][\'name\'] as $_name){
  201.                 $_ext = pathinfo($_name,PATHINFO_EXTENSION);
  202.                 $return = ($return AND in_array($_ext, $_allowExt));
  203.             }
  204.             return $return;
  205.         }else{
  206.             $_ext = pathinfo($_FILE[$_formName],PATHINFO_EXTENSION);
  207.             if(in_array($_ext, $_allowExt)){
  208.                  return TRUE;
  209.             }
  210.             return FALSE;
  211.         }
  212.         return FALSE;
  213.     }
  214.     /**
  215.     * 保存上传文件
  216.     * _formName string  default UploadFile 表单元素name值
  217.     * _savePath string  default NULL       文件保存路径(默认当前路径./upload/目录)
  218.     * _fileName string  default NULL       文件保存名称 默认时间戳+原文件名
  219.     * _chmode   int     default 0664       上传后的文件权限
  220.     */
  221.     public static function save($_formName = \'UploadFile\', $_savePath = NULL, $_fileName = NULL, $_chmode = 0664) {
  222.         $file = \'\';
  223.         $files = array();
  224.         if(is_array($_FILES[$_formName][\'name\'])){
  225.             $_fileName = \'\';
  226.             $files = self::dealFiles($_FILES[$_formName]);
  227.             $fileNames = array();
  228.             foreach($files as $file){
  229.                 $fileNames[] = self::upload($file,$_savePath,$_fileName, $_chmode);
  230.             }
  231.             return $fileNames;
  232.         }else{
  233.             $file = $_FILES[$_formName];
  234.             $fileName = self::upload($file,$_savePath,$_fileName, $_chmode);
  235.         }
  236.         return $fileName;
  237.     }
  238.     /**
  239.     * 保存上传文件
  240.     * 参数同save()
  241.     */
  242.     private static function upload($file = array(), $_savePath = NULL, $_fileName = NULL, $_chmode = 0664) {
  243.         if(empty($file)){ return FALSE;}
  244.         if($_fileName == NULL){
  245.             $_fileName = time().rand(1,100).$file[\'name\'];
  246.         }
  247.         $_fileName = preg_replace(\'/\\s+/\', \'_\', $_fileName);
  248.         if ($_savePath == NULL){
  249.    $_savePath = \'./upload/\';
  250.   }
  251.         self::mkdirs($_savePath,0777);
  252.         $_savePath = rtrim($_savePath, \'/\').\'/\';
  253.         if ( ! is_writable($_savePath)){
  254.    throw new Exception(\'The directory \'.$_savePath.\' is not_writable !\');
  255.         }
  256.         if (is_uploaded_file($file[\'tmp_name\']) AND move_uploaded_file($file[\'tmp_name\'], $filename = $_savePath.$_fileName)){
  257.    if ($chmod !== FALSE){
  258.     chmod($filename, $_chmod);
  259.    }
  260.    return $filename;
  261.   }else{
  262.             throw new Exception(\'Move uploaded File Error!\'.$file[\'tmp_name\']);
  263.         }
  264.         return FALSE;
  265.     }
  266.     // 创建目录树
  267.     public static function mkdirs($dir, $mode = 0777) {
  268.         if (!is_dir($dir)) {
  269.             self::mkdirs(dirname($dir), $mode);
  270.             return mkdir($dir, $mode);
  271.         }
  272.         return TRUE;
  273.     }
  274.     // 多文件上传 数组格式转换
  275.     private static function dealFiles(&$files) {
  276.     $fileArray = array();
  277.     $count = count($files[\'name\']);
  278.     $keys = array_keys($files);
  279.     for ($i=0; $i<$count; $i++) {
  280.      foreach ($keys as $key) {
  281.                if(empty($files[$key][$i])){
  282.                    unset($fileArray[$i][$key]);
  283.                    unset($fileArray[$i][\'error\']);
  284.                }else{
  285.            $fileArray[$i][$key] = $files[$key][$i];
  286.                }
  287.      }
  288.     }
  289.        // 剔除为空的元素
  290.        foreach($fileArray as $key=>$value){
  291.             if(empty($value)){
  292.                 unset($fileArray[$key]);
  293.             }
  294.        }
  295.     return $fileArray;
  296. }
  297. }
  298. // 测试
  299. if( !empty($_FILES[\'UploadFile\'])){
  300.     $allowExt = array(\'jpg\');
  301.     if(FileUpload::valid(\'UploadFile\', $allowExt)){
  302.          $test = FileUpload::save(\'UploadFile\');
  303.          print_r($test);
  304.     }else{
  305.         echo \'error\';
  306.     }
  307. }
  308. ?>
  309. <form action="#" method="post" enctype="multipart/form-data">
  310. <input type="file" name="UploadFile[]"><br/>
  311. <input type="file" name="UploadFile[]"><br/>
  312. <input type="file" name="UploadFile[]"><br/>
  313. <input type="file" name="UploadFile[]"><br/>
  314. <input type="submit" value="sub">
  315. </form>  
复制代码






上一篇:简单的图片处理类
下一篇:php用SMTP发送邮件
authicon huanglv80 发表于 2011-6-18 09:59:41 | 显示全部楼层
顶啦,不错吧
authicon kikiya11 发表于 2011-6-22 11:59:48 | 显示全部楼层
这个贴不错!!!
authicon 计晨 发表于 2011-6-27 02:59:55 | 显示全部楼层
有意思~顶顶 ,继续顶顶。继续顶哦
authicon haidideyu 发表于 2011-6-27 07:00:04 | 显示全部楼层
顶顶更健康
authicon 婷婷爱牛牛 发表于 2011-6-27 14:59:52 | 显示全部楼层
前来看看那
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 01:53

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表