PHP的多文件上传

来源:互联网 发布:淘宝必买清单编辑器 编辑:程序博客网 时间:2024/05/17 06:07

提交表单的页面 

upload.php

[html] view plain copy
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>文件上传</title>  
  6. </head>  
  7. <body>  
  8.     <form action="doAction.php" method="post" enctype="multipart/form-data">  
  9.         请选择您要上传的文件:<input type="file" name="myFile[]"/><br/>  
  10.         请选择您要上传的文件:<input type="file" name="myFile[]"/><br/>  
  11.         请选择您要上传的文件:<input type="file" name="myFile[]"/><br/>  
  12.         请选择您要上传的文件:<input type="file" name="myFile[]"/><br/>  
  13.         请选择您要上传的文件:<input type="file" name="myFile[]"/><br/>  
  14.         请选择您要上传的文件:<input type="file" name="myFile[]"/><br/>  
  15.         <input type="submit" value="上传文件"/>  
  16.     </form>  
  17. </body>  
  18. </html>  

多文件上传类

Upload2.class.php

[php] view plain copy
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: DreamBoy 
  5.  * Date: 2016/4/9 
  6.  * Time: 9:24 
  7.  */  
  8. error_reporting(0);  
  9. class Upload2 {  
  10.     protected $fileName//POST请求时文件的name值  
  11.     protected $maxSize//文件上传的最大大小  
  12.     protected $allowMime//允许上传的文件类型  
  13.     protected $allowExt//允许上传的文件类型  
  14.     protected $uploadPath//文件上传的路径  
  15.     protected $imgFlag//标志是否要求上传的文件为真实图片  
  16.   
  17.     protected $fileInfos//所有文件信息  
  18.     protected $uploadRes//上传文件的结果  
  19.   
  20.     protected $error//记录系统错误号  
  21.     protected $err = array//错误号及错误类型  
  22.         '000' => '文件上传成功',  
  23.         '001' => '超过了PHP配置文件中upload_max_filesize选项值',  
  24.         '002' => '超过了表单中MAX_FILE_SIZE设置的值',  
  25.         '003' => '文件部分被上传',  
  26.         '004' => '没有选择上传文件',  
  27.         '005' => '没有找到临时目录',  
  28.         '006' => '文件不可写',  
  29.         '007' => '由于PHP的扩展程序中断文件上传',  
  30.         '008' => '上传文件过大',  
  31.         '009' => '不允许的文件类型',  
  32.         '010' => '不允许的文件MIME类型',  
  33.         '011' => '文件不是真实图片',  
  34.         '012' => '文件不是通过HTTP POST方式上传上来的',  
  35.         '013' => '文件移动失败',  
  36.         '014' => '系统错误:文件上传出错',  
  37.         );  
  38.   
  39.     /** 
  40.      * Upload2 constructor. 
  41.      * @param string $fileName 
  42.      * @param string $uploadPath 
  43.      * @param bool $imgFlag 
  44.      * @param int $maxSize 
  45.      * @param array $allowExt 
  46.      * @param array $allowMime 
  47.      */  
  48.     public function __construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,  
  49.                                 $allowExt=array('jpeg','jpg','png','gif'),  
  50.                                 $allowMime=array('image/jpeg','image/png','image/gif')) {  
  51.   
  52.         $this->fileName = $fileName;  
  53.         $this->maxSize = $maxSize;  
  54.         $this->allowMime = $allowMime;  
  55.         $this->allowExt = $allowExt;  
  56.         $this->uploadPath = $uploadPath;  
  57.         $this->imgFlag = $imgFlag;  
  58.         $this->fileInfos = $this->getFileInfos();  
  59.     }  
  60.   
  61.     /** 
  62.      * 获取上传的文件信息,并判断上传的文件是单文件还是多文件,设置上传文件的模式 
  63.      * @return mixed 
  64.      */  
  65.     protected function getFileInfos() {  
  66.         if(isset($_FILES[$this->fileName])) {  
  67.             $file = $_FILES[$this->fileName];  
  68.         } else {  
  69.             $this->error = '014';  
  70.             $this->showError();  
  71.         }  
  72.   
  73.         $i = 0;  
  74.         //单文件或者多个单文件上传  
  75.         if(is_string($file['name'])) {  
  76.             $files[$i] = $file;  
  77.         } //多文件上传  
  78.         elseif(is_array($file['name'])) {  
  79.             foreach($file['name'as $key=>$val) {  
  80.                 $files[$i]['name'] = $file['name'][$key];  
  81.                 $files[$i]['type'] = $file['type'][$key];  
  82.                 $files[$i]['tmp_name'] = $file['tmp_name'][$key];  
  83.                 $files[$i]['error'] = $file['error'][$key];  
  84.                 $files[$i]['size'] = $file['size'][$key];  
  85.                 $i++;  
  86.             }  
  87.         }  
  88.         return $files;  
  89.     }  
  90.   
  91.     /** 
  92.      * 显示错误 
  93.      */  
  94.     protected function showError() {  
  95.         $e = $this->err[$this->error];  
  96.         exit('<span style="color:red">' . $e . '</span>');  
  97.     }  
  98.   
  99.     /** 
  100.      * 为序号为$cur的文件设置上传结果信息 
  101.      * @param $cur 
  102.      * @param string $errno 
  103.      */  
  104.     protected function setError($cur$errno='000') {  
  105.         $this->uploadRes[$cur]['errno'] = $errno;  
  106.         $this->uploadRes[$cur]['error'] = $this->err[$errno];  
  107.         $this->uploadRes[$cur]['dest'] = '';  
  108.     }  
  109.   
  110.     /** 
  111.      * 检测上传文件是否出错 
  112.      * @param int $cur 
  113.      * @return bool 
  114.      */  
  115.     protected function checkError($cur=0) {  
  116.         if(is_null($this->fileInfos[$cur])) { //文件获取失败  
  117.             $this->error = '014';  
  118.             $this->showError();  
  119.             return false;  
  120.         }  
  121.   
  122.         if($this->fileInfos[$cur]['error']>0) {  
  123.             switch($this->fileInfos[$cur]['error']) {  
  124.                 case 1:  
  125.                     $curErr = '001';  
  126.                     break;  
  127.                 case 2:  
  128.                     $curErr = '002';  
  129.                     break;  
  130.                 case 3:  
  131.                     $curErr = '003';  
  132.                     break;  
  133.                 case 4:  
  134.                     $curErr = '004';  
  135.                     break;  
  136.                 case 6:  
  137.                     $curErr = '005';  
  138.                     break;  
  139.                 case 7:  
  140.                     $curErr = '006';  
  141.                     break;  
  142.                 case 8:  
  143.                     $curErr = '007';  
  144.                     break;  
  145.             }  
  146.   
  147.             $this->setError($cur$curErr);  
  148.             return false;  
  149.         }  
  150.         return true;  
  151.     }  
  152.   
  153.     /** 
  154.      * 检测上传文件的大小 
  155.      * @param int $cur 
  156.      * @return bool 
  157.      */  
  158.     protected function checkSize($cur=0) {  
  159.         if($this->fileInfos[$cur]['size'] > $this->maxSize) {  
  160.             $this->setError($cur'008');  
  161.             return false;  
  162.         }  
  163.         return true;  
  164.     }  
  165.   
  166.     /** 
  167.      * 获取序号为$cur文件的扩展名 
  168.      * @param int $cur 
  169.      * @return string 
  170.      */  
  171.     protected function getCurExt($cur=0) {  
  172.         return strtolower(pathinfo($this->fileInfos[$cur]['name'], PATHINFO_EXTENSION));  
  173.     }  
  174.   
  175.     /** 
  176.      * 检测文件扩展名 
  177.      * @param int $cur 
  178.      * @return bool 
  179.      */  
  180.     protected function checkExt($cur=0) {  
  181.         $ext = $this->getCurExt($cur);  
  182.         if(!in_array($ext$this->allowExt)) {  
  183.             $this->setError($cur'009');  
  184.             return false;  
  185.         }  
  186.         return true;  
  187.     }  
  188.   
  189.     /** 
  190.      * 检测文件的MIME类型 
  191.      * @param int $cur 
  192.      * @return bool 
  193.      */  
  194.     protected function checkMime($cur=0) {  
  195.         if(!in_array($this->fileInfos[$cur]['type'],$this->allowMime)) {  
  196.             $this->setError($cur'010');  
  197.             return false;  
  198.         }  
  199.         return true;  
  200.     }  
  201.   
  202.     /** 
  203.      * 检测文件是否为真实图片 
  204.      * @param int $cur 
  205.      * @return bool 
  206.      */  
  207.     protected function checkTrueImg($cur=0) {  
  208.         if($this->imgFlag) {  
  209.             if(!@getimagesize($this->fileInfos[$cur]['tmp_name'])) {  
  210.                 $this->setError($cur'011');  
  211.                 return false;  
  212.             }  
  213.         }  
  214.         return true;  
  215.     }  
  216.   
  217.     /** 
  218.      * 检测是否通过HTTP Post方式上传过来的 
  219.      * @param int $cur 
  220.      * @return bool 
  221.      */  
  222.     protected function checkHTTPPost($cur=0) {  
  223.         if(!is_uploaded_file($this->fileInfos[$cur]['tmp_name'])) {  
  224.             $this->error = '012';  
  225.             return false;  
  226.         }  
  227.         return true;  
  228.     }  
  229.   
  230.     /** 
  231.      * 检测目录是否存在,如果不存在则进行创建 
  232.      */  
  233.     protected function checkUploadPath() {  
  234.         if(!file_exists($this->uploadPath)) {  
  235.             mkdir($this->uploadPath, 0777, true);  
  236.         }  
  237.     }  
  238.   
  239.     /** 
  240.      * 产生唯一字符串 
  241.      * @return string 
  242.      */  
  243.     protected function getUniName() {  
  244.         return md5(uniqid(microtime(true),true));  
  245.     }  
  246.   
  247.     /** 
  248.      * 上传文件 
  249.      * @return string 
  250.      */  
  251.     public function uploadFile() {  
  252.         foreach ($this->fileInfos as $key => $value) {  
  253.             if($this->checkError($key) && $this->checkSize($key)  
  254.                 && $this->checkExt($key) && $this->checkMime($key)  
  255.                 && $this->checkTrueImg($key) && $this->checkHTTPPost($key)) {  
  256.   
  257.                 $this->checkUploadPath();  
  258.   
  259.                 $uniName = $this->getUniName();  
  260.                 $ext = $this->getCurExt($key);  
  261.   
  262.                 $destination = $this->uploadPath . '/' . $uniName . '.' . $ext;  
  263.                 if(@move_uploaded_file($this->fileInfos[$key]['tmp_name'], $destination)) {  
  264.                     $this->setError($key);  
  265.                     $this->uploadRes[$key]['dest'] = $destination;  
  266.                 } else {  
  267.                     $this->setError($key'013');  
  268.                 }  
  269.             }  
  270.         }  
  271.   
  272.         return $this->uploadRes;  
  273.     }  
  274. }  


提交文件上传的页面
doAction.php

[php] view plain copy
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: DreamBoy 
  5.  * Date: 2016/4/9 
  6.  * Time: 10:31 
  7.  */  
  8. header('content-type:text/html;charset=utf-8');  
  9. require_once 'Upload2.class.php';  
  10.   
  11. function dump($arr) {  
  12.     echo '<pre>';  
  13.     print_r($arr);  
  14.     echo '</pre>';  
  15. }  
  16.   
  17. //$upload = new Upload();  
  18. //$upload = new Upload('myFile2');  
  19. $upload = new Upload2('myFile');  
  20. $res = $upload->uploadFile();  
  21. dump($res);  

运行:


选择文件:


跳转结果:


跳转后显示的结果信息提示,与我们提交的文件应提示的信息一致。

从中我们也可以看到只有两个文件上传成功。如下:




修改-》 在Upload2.class.php类中增加 一些返回信息:如文件名称,文件类型。提供是否保留文件原名的设置。

[php] view plain copy
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: DreamBoy 
  5.  * Date: 2016/4/9 
  6.  * Time: 9:24 
  7.  */  
  8. error_reporting(0);  
  9. class Upload {  
  10.     protected $fileName//POST请求时文件的name值  
  11.     protected $maxSize//文件上传的最大大小  
  12.     protected $allowMime//允许上传的文件类型  
  13.     protected $allowExt//允许上传的文件类型  
  14.     protected $uploadPath//文件上传的路径  
  15.     protected $imgFlag//标志是否要求上传的文件为真实图片  
  16.     protected $isOldName//标志是否要求上传的文件保留原名  
  17.   
  18.     protected $fileInfos//所有文件信息  
  19.     protected $uploadRes//上传文件的结果  
  20.   
  21.     protected $error//记录系统错误号  
  22.     protected $err = array//错误号及错误类型  
  23.         '000' => '文件上传成功',  
  24.         '001' => '超过了PHP配置文件中upload_max_filesize选项值',  
  25.         '002' => '超过了表单中MAX_FILE_SIZE设置的值',  
  26.         '003' => '文件部分被上传',  
  27.         '004' => '没有选择上传文件',  
  28.         '005' => '没有找到临时目录',  
  29.         '006' => '文件不可写',  
  30.         '007' => '由于PHP的扩展程序中断文件上传',  
  31.         '008' => '上传文件过大',  
  32.         '009' => '不允许的文件类型',  
  33.         '010' => '不允许的文件MIME类型',  
  34.         '011' => '文件不是真实图片',  
  35.         '012' => '文件不是通过HTTP POST方式上传上来的',  
  36.         '013' => '文件移动失败',  
  37.         '014' => '系统错误:文件上传出错',  
  38.         );  
  39.   
  40.     /** 
  41.      * Upload2 constructor. 
  42.      * @param string $fileName 
  43.      * @param string $uploadPath 
  44.      * @param bool $isOldName 
  45.      * @param bool $imgFlag 
  46.      * @param int $maxSize 
  47.      * @param array $allowExt 
  48.      * @param array $allowMime 
  49.      */  
  50.     public function __construct($fileName='myFile',$uploadPath='./uploads',$isOldName=false,$imgFlag=true,  
  51.         $maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif'),  
  52.         $allowMime=array('image/jpeg','image/png','image/gif')) {  
  53.   
  54.         $this->fileName = $fileName;  
  55.         $this->maxSize = $maxSize;  
  56.         $this->allowMime = $allowMime;  
  57.         $this->allowExt = $allowExt;  
  58.         $this->uploadPath = $uploadPath;  
  59.         $this->imgFlag = $imgFlag;  
  60.         $this->isOldName = $isOldName;  
  61.         $this->fileInfos = $this->getFileInfos();  
  62.     }  
  63.   
  64.     /** 
  65.      * 获取上传的文件信息,并判断上传的文件是单文件还是多文件,设置上传文件的模式 
  66.      * @return mixed 
  67.      */  
  68.     protected function getFileInfos() {  
  69.         if(isset($_FILES[$this->fileName])) {  
  70.             $file = $_FILES[$this->fileName];  
  71.         } else {  
  72.             $this->error = '014';  
  73.             $this->showError();  
  74.         }  
  75.   
  76.         $i = 0;  
  77.         //单文件或者多个单文件上传  
  78.         if(is_string($file['name'])) {  
  79.             $files[$i] = $file;  
  80.         } //多文件上传  
  81.         elseif(is_array($file['name'])) {  
  82.             foreach($file['name'as $key=>$val) {  
  83.                 $files[$i]['name'] = $file['name'][$key];  
  84.                 $files[$i]['type'] = $file['type'][$key];  
  85.                 $files[$i]['tmp_name'] = $file['tmp_name'][$key];  
  86.                 $files[$i]['error'] = $file['error'][$key];  
  87.                 $files[$i]['size'] = $file['size'][$key];  
  88.                 $i++;  
  89.             }  
  90.         }  
  91.         return $files;  
  92.     }  
  93.   
  94.     /** 
  95.      * 显示错误 
  96.      */  
  97.     protected function showError() {  
  98.         $e = $this->err[$this->error];  
  99.         exit('<span style="color:red">' . $e . '</span>');  
  100.     }  
  101.   
  102.     /** 
  103.      * 为序号为$cur的文件设置上传结果信息 
  104.      * @param $cur 
  105.      * @param string $errno 
  106.      */  
  107.     protected function setError($cur$errno='000') {  
  108.         $this->uploadRes[$cur]['errno'] = $errno;  
  109.         $this->uploadRes[$cur]['error'] = $this->err[$errno];  
  110.         $this->uploadRes[$cur]['name'] = '';  
  111.         $this->uploadRes[$cur]['dest'] = '';  
  112.         $this->uploadRes[$cur]['type'] = '';  
  113.     }  
  114.   
  115.     /** 
  116.      * 检测上传文件是否出错 
  117.      * @param int $cur 
  118.      * @return bool 
  119.      */  
  120.     protected function checkError($cur=0) {  
  121.         if(is_null($this->fileInfos[$cur])) { //文件获取失败  
  122.             $this->error = '014';  
  123.             $this->showError();  
  124.             return false;  
  125.         }  
  126.   
  127.         if($this->fileInfos[$cur]['error']>0) {  
  128.             switch($this->fileInfos[$cur]['error']) {  
  129.                 case 1:  
  130.                     $curErr = '001';  
  131.                     break;  
  132.                 case 2:  
  133.                     $curErr = '002';  
  134.                     break;  
  135.                 case 3:  
  136.                     $curErr = '003';  
  137.                     break;  
  138.                 case 4:  
  139.                     $curErr = '004';  
  140.                     break;  
  141.                 case 6:  
  142.                     $curErr = '005';  
  143.                     break;  
  144.                 case 7:  
  145.                     $curErr = '006';  
  146.                     break;  
  147.                 case 8:  
  148.                     $curErr = '007';  
  149.                     break;  
  150.             }  
  151.   
  152.             $this->setError($cur$curErr);  
  153.             return false;  
  154.         }  
  155.         return true;  
  156.     }  
  157.   
  158.     /** 
  159.      * 检测上传文件的大小 
  160.      * @param int $cur 
  161.      * @return bool 
  162.      */  
  163.     protected function checkSize($cur=0) {  
  164.         if($this->fileInfos[$cur]['size'] > $this->maxSize) {  
  165.             $this->setError($cur'008');  
  166.             return false;  
  167.         }  
  168.         return true;  
  169.     }  
  170.   
  171.     /** 
  172.      * 获取序号为$cur文件的扩展名 
  173.      * @param int $cur 
  174.      * @return string 
  175.      */  
  176.     protected function getCurExt($cur=0) {  
  177.         return strtolower(pathinfo($this->fileInfos[$cur]['name'], PATHINFO_EXTENSION));  
  178.     }  
  179.   
  180.     /** 
  181.      * 检测文件扩展名 
  182.      * @param int $cur 
  183.      * @return bool 
  184.      */  
  185.     protected function checkExt($cur=0) {  
  186.         $ext = $this->getCurExt($cur);  
  187.         if(!in_array($ext$this->allowExt)) {  
  188.             $this->setError($cur'009');  
  189.             return false;  
  190.         }  
  191.         return true;  
  192.     }  
  193.   
  194.     /** 
  195.      * 检测文件的MIME类型 
  196.      * @param int $cur 
  197.      * @return bool 
  198.      */  
  199.     protected function checkMime($cur=0) {  
  200.         if(!in_array($this->fileInfos[$cur]['type'],$this->allowMime)) {  
  201.             $this->setError($cur'010');  
  202.             return false;  
  203.         }  
  204.         return true;  
  205.     }  
  206.   
  207.     /** 
  208.      * 检测文件是否为真实图片 
  209.      * @param int $cur 
  210.      * @return bool 
  211.      */  
  212.     protected function checkTrueImg($cur=0) {  
  213.         if($this->imgFlag) {  
  214.             if(!@getimagesize($this->fileInfos[$cur]['tmp_name'])) {  
  215.                 $this->setError($cur'011');  
  216.                 return false;  
  217.             }  
  218.         }  
  219.         return true;  
  220.     }  
  221.   
  222.     /** 
  223.      * 检测是否通过HTTP Post方式上传过来的 
  224.      * @param int $cur 
  225.      * @return bool 
  226.      */  
  227.     protected function checkHTTPPost($cur=0) {  
  228.         if(!is_uploaded_file($this->fileInfos[$cur]['tmp_name'])) {  
  229.             $this->error = '012';  
  230.             return false;  
  231.         }  
  232.         return true;  
  233.     }  
  234.   
  235.     /** 
  236.      * 检测目录是否存在,如果不存在则进行创建 
  237.      */  
  238.     protected function checkUploadPath() {  
  239.         if(!file_exists($this->uploadPath)) {  
  240.             mkdir($this->uploadPath, 0777, true);  
  241.         }  
  242.     }  
  243.   
  244.     /** 
  245.      * 产生唯一字符串 
  246.      * @return string 
  247.      */  
  248.     protected function getUniName() {  
  249.         return md5(uniqid(microtime(true),true));  
  250.     }  
  251.   
  252.     /** 
  253.      * 上传文件 
  254.      * @return string 
  255.      */  
  256.     public function uploadFile() {  
  257.         foreach ($this->fileInfos as $key => $value) {  
  258.             if($this->checkError($key) && $this->checkSize($key)  
  259.                 && $this->checkExt($key) && $this->checkMime($key)  
  260.                 && $this->checkTrueImg($key) && $this->checkHTTPPost($key)) {  
  261.   
  262.                 $this->checkUploadPath();  
  263.   
  264.                 if($this->isOldName) {  
  265.                     $name = $this->fileInfos[$key]['name'];  
  266.                     $destination = $this->uploadPath . '/' . $name;  
  267.                 } else {  
  268.                     $name = $this->getUniName();  
  269.                     $ext = $this->getCurExt($key);  
  270.                     $destination = $this->uploadPath . '/' . $name . '.' . $ext;  
  271.                 }  
  272.                   
  273.                 if(@move_uploaded_file($this->fileInfos[$key]['tmp_name'], $destination)) {  
  274.                     $this->setError($key);  
  275.                     $this->uploadRes[$key]['name'] = $name;  
  276.                     $this->uploadRes[$key]['dest'] = $destination;  
  277.                     $this->uploadRes[$key]['type'] = $this->fileInfos[$key]['type'];  
  278.                 } else {  
  279.                     $this->setError($key'013');  
  280.                 }  
  281.             }  
  282.         }  
  283.   
  284.         return $this->uploadRes;  
  285.     }  
  286. }