最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

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

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

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

  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. } 
  46.  
  47. return $file_ary; 
  48. } 
  49.  
  50.  
  51. print "<pre>"; 
  52.  
  53. if ($_files['userfile']) { 
  54. $file_ary = rearrayfiles($_files['userfile']); 
  55.  

(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容