PHP头条
热点:

完整代码演示PHP上传多个文件


前几天看了一本关于PHP的书,让我感触很深,我先介绍一下PHP的发展史,然后在教大家一个PHP上传多个文件的一个小技巧。让我们先来简单的介绍一下PHP吧!PHP 最初是1994年Rasmus Lerdorf创建的,刚刚开始只是一个简单的用Perl语言编写的程序,用来统计他自己网站的访问者。后来又用C语言重新编写,包括可以访问数据库。

  • 关于PHP数组转字符串详细介绍
  • 简单快捷PHP数组赋值方法详解
  • 关于Apache 2.0和PHP5.0安装详解
  • 高手指南PHP安装配置
  • 主流Apache 2 PHP5安装使用介绍
以后越来越多的网站使用了PHP,并且强烈要求增加一些特性,比如循环语句和数组变量等等,在新的成员加入开发行列之后,在1995年 中,PHP2.0发布了。第二版定名为PHP/FI(Form Interpreter)。PHP/FI加入了对mSQL的支持,从此建立了PHP在动态网页开发上的地位。到了1996年底,有15000个网站使用 PHP/FI;时间到了1997年中,使用PHP/FI的网站数字超过五万个。而在1997年中,开始了第三版的开发计划,开发小组加入了 Zeev Suraski 及 Andi Gutmans,而第三版就定名为PHP3。2000年,PHP4.0又问世了,其中增加了许多新的特性。以下给大家介绍一个PHP上传多个文件的方法。

PHP上传多个文件代码实现:

  1. <?php 
  2. require_once("include/upload.class.php");  
  3. if($_POST["button"])  
  4. {  
  5. //print_r($_FILES);  
  6. //多个上传  
  7. //$upload=newTTRUpload($_FILES,"ANY");//同下  
  8.  
  9. $upload=newTTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");  
  10.  
  11. //单个上传  
  12. //$upload=newTTRUpload($_FILES["file1"]);  
  13. $upload->upload();  
  14. echo$upload->getUploadFileName();  
  15. }  
  16. ?> 
  17. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  18. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  19. <head> 
  20. <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> 
  21. <title>UntitledDocument</title> 
  22. </head> 
  23.  
  24. <body> 
  25. <formactionformaction=""method="post"enctype="multipart/form-data"name="form1"id="form1"> 
  26. <inputtypeinputtype="file"name="file1"id="file1"/> 
  27. <br/> 
  28. <inputtypeinputtype="file"name="file2"id="file2"/> 
  29. <br/> 
  30. <inputtypeinputtype="file"name="file3"id="file3"/> 
  31. <br/> 
  32. <inputtypeinputtype="file"name="file4"id="file4"/> 
  33. <br/> 
  34. <inputtypeinputtype="submit"name="button"id="button"value="Submit"/> 
  35. </form> 
  36. </body> 
  37. </html> 
  38.  
  39. <?php 
  40. classTTRUploadextendsError  
  41. {  
  42. constfilesize=81200000;  
  43. private$uploadpath="uploadfile/";  
  44. private$savepath=null;  
  45. private$uploadfilename=null;//单个文件为文件名,批量文件为xxxx|xxxx格式,请注意  
  46. private$ext=array("jpg","gif","png");  
  47. private$error=null;  
  48. private$file=null;  
  49. private$uploadtype=null;  
  50. private$filename=null;  
  51.  
  52. //构造函数,$type:ONE单个上传ANY批量上传;  
  53. publicfunction__construct($file,$type="ONE")  
  54. {  
  55. if($type!="ONE"&&$type!="ANY")  
  56. {  
  57. echo"<scriptlanguagescriptlanguage='javascript'>alert('初始化请选择ONE或者ANY')</script>";  
  58. exit;  
  59. }  
  60. $this->uploadtype=$type;  
  61. $this->file=$file;  
  62. }  
  63.  
  64. privatefunctioncreateFileName()  
  65. {  
  66. return$this->filename="TTR_".time().$this->getRandomN(4);  
  67. }  
  68.  
  69. privatefunctiongetUploadPath()  
  70. {  
  71. if(substr($this->uploadpath,-1,1)!="/")  
  72. {  
  73. $this->savepath=$this->uploadpath."/".date("Ym");  
  74. }else{  
  75. $this->savepath=$this->uploadpath.date("Ym");  
  76. }  
  77. $this->savepath=$this->getFolder($this->savepath);  
  78. returntrue;  
  79. }  
  80.  
  81. privatefunctiongetFileExt($tempfilename)  
  82. {  
  83. returnend(explode(".",$tempfilename));  
  84. }  
  85.  
  86. privatefunctiongetExt()  
  87. {  
  88. if(in_array(strtolower($this->getFileExt($tempfilename)),$this->ext))  
  89. {  
  90. returntrue;  
  91. }else{  
  92. returnfalse;  
  93. }  
  94. }  
  95.  
  96. privatefunctiongetFolder($folder)  
  97. {  
  98. if(!is_dir($folder))  
  99. {  
  100. mkdir($folder);  
  101. }  
  102. return$folder."/";  
  103. }  
  104.  
  105.  
  106. publicfunctionupload()  
  107. {  
  108. if($this->uploadtype=="ONE")  
  109. {  
  110.  
  111.  
  112. if($this->getExt($this->file["type"]))  
  113. {  
  114.  
  115. parent::errorExt();  
  116.  
  117. }elseif($this->file["size"]>self::filesize){  
  118.  
  119. parent::errorFileSize();  
  120.  
  121. }elseif(!$this->getUploadPath()){  
  122.  
  123. parent::errorUploadPath();  
  124.  
  125. }else{  
  126. $filenametemp=$this->createFileName();  
  127. $filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file["name"]);  
  128. if(move_uploaded_file($this->file["tmp_name"],$filename))  
  129. {  
  130. $this->uploadfilename=$filenametemp;  
  131. parent::okMoved();  
  132.  
  133.  
  134. }else{  
  135. parent::errorMoveUpload();  
  136. }  
  137. }  
  138. }elseif($this->uploadtype=="ANY"){  
  139.  
  140. for($i=0;$i<count($this->file);$i++)  
  141. {  
  142.  
  143. if($this->getExt($this->file[$i]["type"]))  
  144. {  
  145. parent::errorExt();  
  146.  
  147. }elseif($this->file[$i]["size"]>self::filesize){  
  148.  
  149. parent::errorFileSize();  
  150.  
  151. }elseif(!$this->getUploadPath()){  
  152.  
  153. parent::errorUploadPath();  
  154.  
  155. }else{  
  156. $filenametemp=$this->createFileName();  
  157. $filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file[$i]["name"]);  
  158. if(move_uploaded_file($this->file[$i]["tmp_name"],$filename))  
  159. {  
  160. $str.=$filenametemp."|";  
  161.  
  162. }else{  
  163. parent::errorMoveUpload();  
  164. }  
  165.  
  166. }  
  167.  
  168. }  
  169. $this->uploadfilename=substr($str,0,strlen($str)-1);  
  170. parent::okMoved();  
  171. }  
  172. }  
  173.  
  174. publicfunctiongetUploadFileName()  
  175. {  
  176. return$this->uploadfilename;  
  177. }  
  178.  
  179. publicfunctionsetUploadPath($path)  
  180. {  
  181. $this->uploadpath=$path;  
  182. }  
  183.  
  184.  
  185. privatefunctiongetRandomN($n)  
  186. {  
  187. if($n<1||$n>10)return"";  
  188.  
  189. $ary_num=array(0,1,2,3,4,5,6,7,8,9);  
  190. $return="";  
  191. for($i=0;$i<$n;$i++)  
  192. {  
  193. $randrandn=rand(0,9-$i);  
  194. $return.=$ary_num[$randn];  
  195. $ary_num[$randn]=$ary_num[9-$i];  
  196. }  
  197. return$return;  
  198. }  
  199.  
  200.  
  201.  
  202. publicfunction__destruct()  
  203. {  
  204. $this->uploadfilename=null;  
  205. $this->uploadtype=null;  
  206. $this->file=null;  
  207. $this->savepath=null;  
  208. }  
  209.  
  210. }  
  211.  
  212. classError  
  213. {  
  214. publicstaticfunctionerrorFileSize()  
  215. {  
  216. echo"超出最大上传限制";  
  217. }  
  218.  
  219. publicstaticfunctionerrorExt()  
  220. {  
  221. echo"此类文件不允许上传";  
  222. }  
  223.  
  224. publicstaticfunctionerrorUploadPath()  
  225. {  
  226. echo"上传路径不正确";  
  227. }  
  228.  
  229. publicstaticfunctionerrorMoveUpload()  
  230. {  
  231. echo"上传失败";  
  232. }  
  233.  
  234. publicstaticfunctionokMoved()  
  235. {  
  236. echo"上传成功!";  
  237. }  
  238.  
  239. publicstaticfunctionokArrayMoved()  
  240. {  
  241. echo"上传成功!";  

www.phpzy.comtrue/php/16478.htmlTechArticle完整代码演示PHP上传多个文件 前几天看了一本关于PHP的书,让我感触很深,我先介绍一下PHP的发展史,然后在教大家一个PHP上传多个文件的一个小技巧。让我们先来简单的介绍一下PH...

相关文章

相关频道:

PHP之友评论

今天推荐