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

php教程评论311 views阅读模式

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

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

企鹅博客
  • 本文由 发表于 2020年1月16日 04:55:39
  • 转载请务必保留本文链接:https://www.qieseo.com/306443.html

发表评论