网站地图    收藏   

主页 > php专栏 > php上传下载 >

php 实现多文件上传程序代码 - php上传下载

来源:自学PHP网    时间:2014-11-28 00:00 作者: 阅读:

[导读] php文件上传与多文件上传其它没有多大的区别,多文件上传只是我们把表单名改成数组形式,而获取利用foreach遍历就可以实现多文件上传了,动态多文件上传只要在js加一个动态增加多文件...

php 实现多文件上传程序代码

php文件上传与多文件上传其它没有多大的区别,多文件上传只是我们把表单名改成数组形式,而获取利用foreach遍历就可以实现多文件上传了,动态多文件上传只要在js加一个动态增加多文件上传框,同样在php处理时就遍历一下数组就成了.

最简单的实例如下,代码如下:

  1. <form action="" method="post" enctype="multipart/form-data"
  2. <p>Pictures: 
  3. <input type="file" name="pictures[]" /> 
  4. <input type="file" name="pictures[]" /> 
  5. <input type="file" name="pictures[]" /> 
  6. <input type="submit" value="Send" /> 
  7. </p> 
  8. </form> 
  9.  
  10.        <?php 
  11. foreach ($_FILES["pictures"]["error"as $key => $error) { 
  12.         if ($error == UPLOAD_ERR_OK) { 
  13.             $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; 
  14.             $name = $_FILES["pictures"]["name"][$key]; 
  15.             move_uploaded_file($tmp_name"data/$name"); 
  16.         } 
  17. ?> 

下面分享其它朋友的例子,代码如下:

  1. <?php 
  2. //filename:multi_upload.php 
  3. if($ifupload
  4. $path=AddSlashes(dirname($PATH_TRANSLATED))."\upload\"
  5. for($i=1;$i<=8;$i++) 
  6. $files="afile".$i
  7. if(${$files}!="none"
  8. if(copy(${$files},$path.${$files."_name"})) 
  9. print "<b>You have uploaded files successfully</b><br>"
  10. print "<a href="multi_upload.php">Return</a>"
  11. exit
  12. ?> 
  13. <html> 
  14. <html> 
  15. <head> 
  16. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"
  17. <meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"
  18. <title>多个文件上传</title> 
  19. <style type="text/css"
  20. <!-- 
  21. BODY 
  22. PADDING-RIGHT: 0px; 
  23. MARGIN-TOP: 0px; 
  24. PADDING-LEFT: 0px; 
  25. FONT-SIZE: 8px; 
  26. MARGIN-LEFT: 0px; 
  27. CURSOR: default
  28. COLOR: black; 
  29. MARGIN-RIGHT: 0px; 
  30. PADDING-TOP: 0px; 
  31. FONT-FAMILY: Arial; 
  32. BACKGROUND-COLOR: transparent; 
  33. TEXT-ALIGN: center 
  34. .TxtInput 
  35. FONT-SIZE: 8pt; 
  36. WIDTH: 100%; 
  37. CURSOR: default
  38. COLOR: black; 
  39. FONT-FAMILY: Arial; 
  40. HEIGHT: 21px; 
  41. BACKGROUND-COLOR: white; 
  42. TEXT-ALIGN: left 
  43. .FieldLabel 
  44. FONT-WEIGHT: normal; 
  45. FONT-SIZE: 9pt; 
  46. WIDTH: 100%; 
  47. COLOR: black; 
  48. FONT-FAMILY: Arial; 
  49. BACKGROUND-COLOR: transparent; 
  50. TEXT-ALIGN: left 
  51. .HeadBtn 
  52. BORDER-RIGHT: black 1px solid; 
  53. BORDER-TOP: white 1px solid; 
  54. FONT-SIZE: 8pt; 
  55. OVERFLOW: hidden; 
  56. BORDER-LEFT: white 1px solid; 
  57. WIDTH: 70px; 
  58. COLOR: black; 
  59. BORDER-BOTTOM: black 1px solid; 
  60. FONT-FAMILY: Arial; 
  61. HEIGHT: 21px; 
  62. BACKGROUND-COLOR: #8e8dcd; 
  63. TEXT-ALIGN: center 
  64. .TransEx 
  65. BORDER-RIGHT: black 1px solid; 
  66. PADDING-RIGHT: 8px; 
  67. BORDER-TOP: white 1px solid; 
  68. PADDING-LEFT: 8px; 
  69. FONT-SIZE: 8pt; 
  70. PADDING-BOTTOM: 3px; 
  71. BORDER-LEFT: white 1px solid; 
  72. WIDTH: 720px; 
  73. PADDING-TOP: 3px; 
  74. BORDER-BOTTOM: black 1px solid; 
  75. FONT-FAMILY: Arial; 
  76. BACKGROUND-COLOR: #c0c0c0; 
  77. TEXT-ALIGN: center 
  78. --> 
  79. </style> 
  80. <script language="javascript"
  81. function window.onload() 
  82. document.forms[0].btnOk.onclick=btn_ok; 
  83. function btn_ok() 
  84. for(var i=1;i<=8;i++) 
  85. if(eval("document.forms[0].afile"+i+".value!=''")) 
  86. document.forms[0].submit(); 
  87. return true; 
  88. alert("None of file have been selected"); 
  89. return false; 
  90. </script> 
  91. </head> 
  92. <body> 
  93. <form method="post" action="multi_upload.php" name="frmUpload" enctype="multipart/form-data" > 
  94. <table id="divContainer" style="HEIGHT: 100%; WIDTH: 380" border="0"
  95. <tr height="35"><td align="right" valign="bottom">多文件上传 </td></tr> 
  96. <tr><td align="center" valign="top"
  97. <table class="Transex" border="0" cellspacing="0" cellpadding="0" style="WIDTH: 360px"
  98. <tr style="HEIGHT: 10px" > 
  99. <td style="WIDTH: 5px"></td> 
  100. <td colspan="2"></td>  
  101. <td style="WIDTH: 5px"></td> 
  102. </tr> 
  103. <tr> 
  104. <td></td> 
  105. <td nowrap><label class="FieldLabel"> 文件1</label></td> 
  106. <td><input type="file" class="TxtInput" tabindex="1" name="afile1" style="WIDTH: 282px"></td> 
  107. <td></td> 
  108. </tr> 
  109. <tr> 
  110. <td></td> 
  111. <td nowrap><label class="FieldLabel"> 文件2</label></td> 
  112. <td><input type="file" class="TxtInput" tabindex="2" name="afile2" style="WIDTH: 282px"></td> 
  113. <td></td> 
  114. </tr> 
  115. <tr> 
  116. <td></td> 
  117. <td nowrap><label class="FieldLabel"> 文件3</label></td> 
  118. <td><input type="file" class="TxtInput" tabindex="3" name="afile3" style="WIDTH: 282px"></td> 
  119. <td></td> 
  120. </tr> 
  121. <tr> 
  122. <td></td> 
  123. <td nowrap><label class="FieldLabel"> 文件4</label></td> 
  124. <td><input type="file" class="TxtInput" tabindex="4" name="afile4" style="WIDTH: 282px"></td> 
  125. <td></td> 
  126. </tr> 
  127. <tr> 
  128. <td></td> 
  129. <td nowrap><label class="FieldLabel"> 文件5</label></td> 
  130. <td><input type="file" class="TxtInput" tabindex="5" name="afile5" style="WIDTH: 282px"></td> 
  131. <td></td> 
  132. </tr> 
  133. <tr> 
  134. <td></td> 
  135. <td nowrap><label class="FieldLabel"> 文件6</label></td> 
  136. <td><input type="file" class="TxtInput" tabindex="6" name="afile6" style="WIDTH: 282px"></td> 
  137. <td></td> 
  138. </tr> 
  139. <tr> 
  140. <td></td> 
  141. <td nowrap><label class="FieldLabel"> 文件7</label></td> 
  142. <td><input type="file" class="TxtInput" tabindex="7" name="afile7" style="WIDTH: 282px"></td> 
  143. <td></td> 
  144. </tr> 
  145. <tr> 
  146. <td></td> 
  147. <td nowrap><label class="FieldLabel"> 文件8</label></td> 
  148. <td><input type="file" class="TxtInput" tabindex="8" name="afile8" style="WIDTH: 282px"></td>//开源代码phpfensi.com 
  149. <td></td> 
  150. </tr> 
  151. <tr style="HEIGHT: 5px"
  152. <td style="WIDTH: 5px"
  153. <td style="WIDTH: 350px" colspan="2"><hr width="100%"></td>  
  154. <td style="WIDTH: 5px"></td> 
  155. </tr> 
  156. <tr> 
  157. <td></td> 
  158. <td colspan="2" align="left"
  159. <button tabindex="5" class="headbtn" align="center" name="btnOk" id="btnOk" accesskey="O">确定(<ins>O</ins>)</button> 
  160. <input type="hidden" name="ifupload" value=1> 
  161. <button tabindex="5" class="headbtn" align="center" name="btnCancel" id="btnCancel" accesskey="C" onclick="window.close();">取消(<ins>C</ins>)</button></td>  
  162. <td></td> 
  163. </tr> 
  164. <tr style="HEIGHT: 5px"
  165. <td style="WIDTH: 5px"
  166. <td style="WIDTH: 350px" colspan="2"></td>  
  167. <td style="WIDTH: 5px"></td> 
  168. </tr> 
  169. </table> 
  170. </td> 
  171. </tr> 
  172. </table> 
  173. </form> 
  174. </body> 
  175. </html> 

如果我们要动态不确定性的多文件上传怎么实现下面也有实例,代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>文档上传</title>  
  6. </head>  
  7. <body>  
  8. <script language="javascript"><!--  
  9. 动态添加文件选择控件-->  
  10. function AddRow()  
  11. {  
  12. var eNewRow = tblData.insertRow();  
  13. for (var i=0;i<1;i++)  
  14. {  
  15. var eNewCell = eNewRow.insertCell();  
  16. eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";  
  17. }  
  18. }  
  19. // --></script>  
  20. <form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >  
  21. <table id="tblData" width="400" border="0">  
  22. <!-- 将上传文件必须用post的方法和enctype="multipart/form-data" -->  
  23. <!-- 将本页的网址传给uploadfile.php-->  
  24. <input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />  
  25. <tr><td>文件上传列表  
  26. <input type="button" name="addfile" onclick="AddRow()" value="添加列表" /></td></tr>  
  27. <!-- filelist[]必须是一个数组-->  
  28. <tr><td><input type="file" name="filelist[]" size="50" /></td></tr>  
  29. </table>  
  30. <input type="submit" name="submitfile" value="提交文件" />  
  31. </form>  
  32. </body>  
  33. </html>  
  34. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  35. <html xmlns="http://www.w3.org/1999/xhtml">  
  36. <head>  
  37. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  38. <title>文档上传</title>  
  39. </head>  
  40. <body>  
  41. <script language="javascript"><!--  
  42. 动态添加文件选择控件-->  
  43. function AddRow()  
  44. {  
  45. var eNewRow = tblData.insertRow();  
  46. for (var i=0;i<1;i++)  
  47. {  
  48. var eNewCell = eNewRow.insertCell();  
  49. eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";  
  50. }  
  51. }  
  52. // --></script>  
  53. <form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >  
  54. <table id="tblData" width="400" border="0">  
  55. <!-- 将上传文件必须用post的方法和enctype="multipart/form-data" -->  
  56. <!-- 将本页的网址传给uploadfile.php-->  
  57. <input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />  
  58. <tr><td>文件上传列表  
  59. <input type="button" name="addfile" onclick="AddRow()" value="添加列表" /></td></tr>  
  60. <!-- filelist[]必须是一个数组-->  
  61. <tr><td><input type="file" name="filelist[]" size="50" /></td></tr>  
  62. </table>  
  63. <input type="submit" name="submitfile" value="提交文件" />  
  64. </form> 
  65. </body> 
  66. </html> 

提交文件代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>文件上传结果</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9. if ($_POST["submitfile"]!="")  
  10. {  
  11. $Path="./".date('Ym')."/";  
  12. if (!is_dir($Path))//创建路径  
  13. mkdir($Path); }  
  14. echo "<div>";  
  15. for ($i=0;$i<count($filelist);$i++)  
  16. //$_FILES["filelist"]["size"][$i]的排列顺序不可以变,因为fileist是一个二维数组  
  17. if ($_FILES["filelist"]["size"][$i]!=0)  
  18. {  
  19. $File=$Path.date('Ymdhm')."_".$_FILES["filelist"]["name"][$i];  
  20. if (move_uploaded_file($_FILES["filelist"]["tmp_name"][$i],$File))  
  21. echo "文件上传成功 文件类型:".$_FILES["filelist"]["type"][$i]." "."文件名:"  
  22. .$_FILES["filelist"]["name"][$i]."<br>"; }  
  23. else  
  24. echo "文件名:".$_FILES["filelist"]["name"][$i]."上传失败</br>"; }  
  25. }  
  26. }  
  27. echo "</div><br><a href="$postadd" href="$postadd">返回</a></div>";  
  28. }  
  29. ?>  
  30. </body>  
  31. </html> 

另:错误信息说明

从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码,该代码可以在文件上传时生成的文件数组中的 error 字段中被找到,也就是 $_FILES['userfile']['error']。 

UPLOAD_ERR_OK

其值为 0,没有错误发生,文件上传成功.

UPLOAD_ERR_INI_SIZE

其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值.

UPLOAD_ERR_FORM_SIZE

其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值.UPLOAD_ERR_PARTIAL

其值为 3,文件只有部分被上传.

UPLOAD_ERR_NO_FILE

其值为 4,没有文件被上传.

UPLOAD_ERR_NO_TMP_DIR

其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进.

UPLOAD_ERR_CANT_WRITE

其值为 7,文件写入失败。PHP 5.1.0 引进.

注意:以上值在 PHP 4.3.0 之后变成了 PHP 常量.

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论