服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

三款php多文件上传实例代码

时间:2016-05-08 10:37来源: 作者: 点击:
在php开发应用中经常会碰到文件上传,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多文件上传实例代码,好了费话不说多了来看看这些多文件上传功能适合你么. 示例代

在php开发应用中经常会碰到文件上传,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多文件上传实例代码,好了费话不说多了来看看这些多文件上传功能适合你么.

示例代码如下:

  1. <form action="" method="post" enctype="multipart/form-data" > 
  2. <input type="file" name="uploadfile[]">   命名必是这样有"[]" 
  3. <input type="file" name="uploadfile[]">   
  4.  
  5. //设置允许用户上传的文件类型。 
  6. $type = array('gif''jpg''png''zip''rar'); 
  7. $upload = new uploadfile($_files['uploadfile'], '/', 1024*1024, $type); 
  8. 参数说明:1:表单的文件,2:上传目录,3:支持文件大小,4:允许文件类型 
  9. $icount = $upload->upload(); 
  10. if($icount > 0) { //上传成功 
  11.    print_r($upload->getsaveinfo()); 
  12.   */ 
  13.  
  14. class uploadfile { 
  15. var $postfile = array();       // 用户上传的文件 
  16. var $custompath = "";          // 自定义文件上传路径 
  17. var $maxsize = "";             // 文件最大尺寸 
  18. var $lasterror = "";           // 最后一次出错信息 
  19. var $allowtype = array('gif''jpg''png''zip''rar''txt''doc''pdf'); 
  20. var $endfilename = "";         // 最终保存的文件名 
  21. var $saveinfo = array();       // 保存文件的最终信息 
  22. var $root_dir = ""// 项目在硬盘上的位置 
  23.  
  24. /** 
  25. * 构造函数 
  26. * @access public 
  27. */ 
  28.  
  29. function uploadfile($arrfile$path="_"$size = 2097152, $type = 0) { 
  30.    $this->postfile     = $arrfile
  31.    $this->custompath   = $path == "_" ? "" : $path ; 
  32.    $this->maxsize      = $size
  33.    if($type!=0)   $this->allowtype   = $arrfile
  34.    $this->root_dir      = $_server['document_root']; 
  35.    $this->_mkdir($this->custompath);  
  36. }  
  37.  
  38. /** 
  39. * 文件上传的核心代码 
  40. * @access public 
  41. * @return int 上传成功文件数 
  42. */ 
  43.  
  44. function upload() { 
  45.    $ilen = sizeof($this->postfile['name']); 
  46.    for($i=0;$i<$ilen;$i++){ 
  47.     if ($this->postfile['error'][$i] == 0) { //上传时没有发生错误 
  48.       //取当前文件名、临时文件名、大小、扩展名,后面将用到。 
  49.      $sname   = $this->postfile['name'][$i]; 
  50.      $stname = $this->postfile['tmp_name'][$i]; 
  51.      $isize   = $this->postfile['size'][$i]; 
  52.      $stype   = $this->postfile['type'][$i]; 
  53.      $sextn   = $this->_getextname($sname); 
  54.      //开源代码最模板zuimoban.com 
  55.      
  56.      //检测当前上传文件大小是否合法。 
  57.      if($this->_checksize){ 
  58.       $this->lasterror = "您上传的文件[".$sname."],超过系统支持大小!"
  59.       $this->_showmsg($this->lasterror); 
  60.       continue
  61.      } 
  62.  
  63.      if(!is_uploaded_file($stname)) { 
  64.       $this->lasterror = "您的文件不是通过正常途径上传!"
  65.       $this->_showmsg($this->lasterror); 
  66.       continue
  67.      } 
  68.      $_filename = basename($sname,".".$sextn)."_".time().".".$sextn
  69.      $this->endfilename = $this->custompath.$_filename
  70.      
  71.      if(!move_uploaded_file($stname$this->root_dir.$this->endfilename)) { 
  72.       $this->lasterror = $this->postfile['error'][$i]; 
  73.       $this->_showmsg($this->lasterror); 
  74.       continue
  75.      } 
  76.  
  77.      //存储当前文件的有关信息,以便其它程序调用。 
  78.      $this->save_info[] =   array("name" => $sname"type" => $sextn"size" => $isize,   "path" => $this->endfilename); 
  79.     } 
  80.    } 
  81.  
  82.    return sizeof($this->save_info); 
  1. /** 
  2. * 得到上传信息 
  3. * @access public 
  4. * @return array 保存信息 
  5. */ 
  6. function getsaveinfo(){ 
  7.    return $this->save_info; 
  8.  
  9.  
  10.  
  11.  
  12. /** 
  13. * 得到文件名的扩展名 
  14. * @access private 
  15. * @return string 返回文件扩展名 
  16. */ 
  17. private function _getextname($filename){ 
  18.    $arrparts = pathinfo($filename); 
  19.    return $arrparts['extension']; 
  20.  
  21. /** 
  22. * 判断文件大小 
  23. * @access private 
  24. * @return boolean 传入size大于系统定义,则true,反之false 
  25. */ 
  26. private function _checksize($size){ 
  27.    return $size > $this->maxsize; 
  28.  
  29. /** 
  30. * 输出错误信息 
  31. * @access private 
  32. * @return void 
  33. */ 
  34. private function _showmsg($msg){ 
  35.    printf("<b><错误信息:></b> %s <br>n"$msg); 
  36.  
  37. /** 
  38. * 新增多级目录 
  39. * @access private 
  40. * @return void 
  41. */ 
  42. private function _mkdir($p){ 
  43.    $ap = split('[/]'$p); 
  44.    foreach($ap as $v){ 
  45.     if(!emptyempty($v)){ 
  46.      if(emptyempty($path)) $path=$v
  47.      else $path.='/'.$v
  48.      file_exists($this->root_dir."/".$pathor mkdir($this->root_dir."/".$path); 
  49.     } 
  50.    } 
  51.  
  52. /* 
  53.  
  54.  
  55. } 
  56.  
  57.  
  58.  
  59. /**//* 
  60. //注意,上传组件name属性不管是一个还是多个都要使用数组形式,如: 
  61. <input type="file" name="user_upload_file[]">  
  62. <input type="file" name="user_upload_file[]"> 
  63.  
  64. //如果用户点击了上传按钮。 
  65. if ($_post['action'] == "上传") { 
  66. //设置允许用户上传的文件类型。 
  67. $type = array('gif', 'jpg', 'png', 'zip', 'rar'); 
  68. //实例化上传类,第一个参数为用户上传的文件组、第二个参数为存储路径、 
  69. //第三个参数为文件最大大小。如果不填则默认为2m 
  70. //第四个参数为充许用户上传的类型数组。如果不填则默认为gif, jpg, png, zip, rar, txt, doc, pdf 
  71. $upload = new uploadfile($_files['user_upload_file'], 'j:/tmp', 100000, $type); 
  72. //上传用户文件,返回int值,为上传成功的文件个数。 
  73. $num = $upload->upload(); 
  74. if ($num != 0) { 
  75. echo "上传成功<br>"; 
  76. //取得文件的有关信息,文件名、类型、大小、路径。用print_r()打印出来。 
  77. print_r($upload->getsaveinfo()); 
  78.  
  79. echo $num."个文件上传成功"; 
  80. } 
  81. else { 
  82. echo "上传失败<br>"; 
  83. } 
  84. } 
  85.  
  86.  
  87. * @(#)uploadfile.php 
  88.  
  89. * 可同时处理用户多个上传文件。效验文件有效性后存储至指定目录。 
  90. * 可返回上传文件的相关有用信息供其它程序使用。(如文件名、类型、大小、保存路径) 
  91. * 使用方法请见本类底部(uploadfile类使用注释)信息。 
  92. * 
  93. */ 
  94. class uploadfile { 
  95.  
  96. var $user_post_file = array(); //用户上传的文件 
  97. var $save_file_path;    //存放用户上传文件的路径 
  98. var $max_file_size;     //文件最大尺寸 
  99. var $last_error;     //记录最后一次出错信息 
  100. //默认允许用户上传的文件类型 
  101. var $allow_type = array('gif''jpg''png''zip''rar''txt''doc''pdf'); 
  102. var $final_file_path//最终保存的文件名 
  103.  
  104. var $save_info = array(); //返回一组有用信息,用于提示用户。 
  105.  
  106. /**//** 
  107. * 构造函数,用与初始化相关信息,用户待上传文件、存储路径等 
  108. * 
  109. * @param array $file 用户上传的文件 
  110. * @param string $path 存储用户上传文件的路径 
  111. * @param integer $size 允许用户上传文件的大小(字节) 
  112. * @param array $type   此数组中存放允计用户上传的文件类型 
  113. */ 
  114. function uploadfile($file$path$size = 2097152, $type = '') { 
  115. $this->user_post_file = $file
  116. $this->save_file_path = $path
  117. $this->max_file_size = $size//如果用户不填写文件大小,则默认为2m. 
  118. if ($type != ''
  119.    $this->allow_type = $type
  120.  
  121. /**//** 
  122. * 存储用户上传文件,检验合法性通过后,存储至指定位置。 
  123. * @access public 
  124. * @return int    值为0时上传失败,非0表示上传成功的个数。 
  125. */ 
  126. function upload() { 
  127.  
  128. for ($i = 0; $i < count($this->user_post_file['name']); $i++) { 
  129.    //如果当前文件上传功能,则执行下一步。 
  130.    if ($this->user_post_file['error'][$i] == 0) { 
  131.     //取当前文件名、临时文件名、大小、扩展名,后面将用到。 
  132.     $name = $this->user_post_file['name'][$i]; 
  133.     $tmpname = $this->user_post_file['tmp_name'][$i]; 
  134.     $size = $this->user_post_file['size'][$i]; 
  135.     $mime_type = $this->user_post_file['type'][$i]; 
  136.     $type = $this->getfileext($this->user_post_file['name'][$i]); 
  137.     //检测当前上传文件大小是否合法。 
  138.     if (!$this->checksize($size)) { 
  139.      $this->last_error = "the file size is too big. file name is: ".$name
  140.      $this->halt($this->last_error); 
  141.      continue
  142.     } 
  143.     //检测当前上传文件扩展名是否合法。 
  144.     if (!$this->checktype($type)) { 
  145.      $this->last_error = "unallowable file type: .".$type." file name is: ".$name
  146.      $this->halt($this->last_error); 
  147.      continue
  148.     } 
  149.     //检测当前上传文件是否非法提交。 
  150.     if(!is_uploaded_file($tmpname)) { 
  151.      $this->last_error = "invalid post file method. file name is: ".$name
  152.      $this->halt($this->last_error); 
  153.      continue
  154.     } 
  155.     //移动文件后,重命名文件用。 
  156.     $basename = $this->getbasename($name".".$type); 
  157.     //移动后的文件名 
  158.     $saveas = $basename."-".time().".".$type
  159.     //组合新文件名再存到指定目录下,格式:存储路径 + 文件名 + 时间 + 扩展名 
  160.     $this->final_file_path = $this->save_file_path."/".$saveas
  161.     if(!move_uploaded_file($tmpname$this->final_file_path)) { 
  162.      $this->last_error = $this->user_post_file['error'][$i]; 
  163.      $this->halt($this->last_error); 
  164.      continue
  165.     } 
  166.     //存储当前文件的有关信息,以便其它程序调用。 
  167.     $this->save_info[] = array("name" => $name"type" => $type
  168.            "mime_type" => $mime_type
  169.                              "size" => $size"saveas" => $saveas
  170.                              "path" => $this->final_file_path); 
  171.    } 
  172. return count($this->save_info); //返回上传成功的文件数目 
  173.  
  174. /**//** 
  175. * 返回一些有用的信息,以便用于其它地方。 
  176. * @access public 
  177. * @return array 返回最终保存的路径 
  178. */ 
  179. function getsaveinfo() { 
  180. return $this->save_info; 
  181.  
  182. /**//** 
  183. * 检测用户提交文件大小是否合法 
  184. * @param integer $size 用户上传文件的大小 
  185. * @access private 
  186. * @return boolean 如果为true说明大小合法,反之不合法 
  187. */ 
  188. function checksize($size) { 
  189. if ($size > $this->max_file_size) { 
  190.    return false; 
  191. else { 
  192.    return true; 
  193.  
  194. /**//** 
  195. * 检测用户提交文件类型是否合法 
  196. * @access private 
  197. * @return boolean 如果为true说明类型合法,反之不合法 
  198. */ 
  199. function checktype($extension) { 
  200. foreach ($this->allow_type as $type) { 
  201.    if (strcasecmp($extension , $type) == 0) 
  202.     return true; 
  203. return false; 
  204.  
  205. /**//** 
  206. * 显示出错信息 
  207. * @param $msg    要显示的出错信息      
  208. * @access private 
  209. */ 
  210. function halt($msg) { 
  211. printf("<b><uploadfile error:></b> %s <br>n"$msg); 
  212.  
  213. /**//** 
  214. * 取文件扩展名 
  215. * @param string $filename 给定要取扩展名的文件 
  216. * @access private 
  217. * @return string      返回给定文件扩展名 
  218. */ 
  219. function getfileext($filename) { 
  220. $stuff = pathinfo($filename); 
  221. return $stuff['extension']; 
  222. /**//** 
  223. * 取给定文件文件名,不包括扩展名。 
  224. * eg: getbasename("j:/hexuzhong.jpg"); //返回 hexuzhong 
  225.  
  226. * @param string $filename 给定要取文件名的文件 
  227. * @access private 
  228. * @return string 返回文件名 
  229. */ 
  230. function getbasename($filename$type) { 
  231. $basename = basename($filename$type); 
  232. return $basename
  233. /**//******************** uploadfile类使用注释 
  234.  
  235.  
  236. */ 
  237. ?> 

一个简单实例,代码如下:

  1. <form enctype="multipart/form-data" action="up.php" method="post"
  2. send this file: <input name="userfile[]" type="file" /><br> 
  3. send this file: <input name="userfile[]" type="file" /><br> 
  4. send this file: <input name="userfile[]" type="file" /><br> 
  5. <input type="submit" value="send file" /> 
  6. </form> 
  7.  
  8.  
  9. <?php 
  10. // in php versions earlier than 4.1.0, $http_post_files should be used instead 
  11. // of $_files. 
  12.  
  13. $uploaddir = './'
  14. /* 
  15. print "<pre>"; 
  16.  
  17.  
  18. if (move_uploaded_file($_files['userfile']['tmp_name'], $uploadfile)) { 
  19. print "file is valid, and was successfully uploaded. "; 
  20. print "here's some more debugging info:n"; 
  21. print_r($_files); 
  22. } else { 
  23. print "possible file upload attack! here's some debugging info:n"; 
  24. print_r($_files); 
  25. } 
  26.  
  27.  
  28. print "</pre>"; 
  29. */ 
  30. ?> 
  31.  
  32.  
  33. <?php 
  34.  
  35. function rearrayfiles(&$file_post) { 
  36.  
  37. $file_ary = array(); 
  38. $file_count = count($file_post['name']); 
  39. $file_keys = array_keys($file_post); 
  40.  
  41. for ($i=0; $i<$file_count$i++) { 
  42. foreach ($file_keys as $key) { 
  43. $file_ary[$i][$key] = $file_post[$key][$i]; 
  44.  
  45. return $file_ary
  46.  
  47.  
  48. print "<pre>"
  49.  
  50. if ($_files['userfile']) { 
  51. $file_ary = rearrayfiles($_files['userfile']); 
  52.  

(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容