国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

三款php多文件上傳實例代碼

2024-09-04 11:45:04
字體:
來源:轉載
供稿:網友

在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.      //開源代碼Vevb.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']); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝州市| 南通市| 洪泽县| 清水县| 北京市| 远安县| 会宁县| 贵阳市| 郎溪县| 双流县| 平乐县| 阿拉善左旗| 舞钢市| 巢湖市| 镇赉县| 容城县| 南汇区| 惠东县| 海阳市| 黄浦区| 科尔| 揭阳市| 永和县| 留坝县| 调兵山市| 离岛区| 新安县| 乐业县| 承德市| 宣威市| 巴东县| 个旧市| 岚皋县| 壶关县| 永登县| 翁源县| 东乌珠穆沁旗| 于都县| 巴青县| 凤山县| 岳普湖县|