自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

完整代碼演示PHP上傳多個(gè)文件

開發(fā) 后端
文章詳細(xì)的介紹了PHP的發(fā)展歷程和教大家一個(gè)PHP上傳多個(gè)文件的小技巧,希望對(duì)研究php的朋友有幫助。

前幾天看了一本關(guān)于PHP的書,讓我感觸很深,我先介紹一下PHP的發(fā)展史,然后在教大家一個(gè)PHP上傳多個(gè)文件的一個(gè)小技巧。讓我們先來(lái)簡(jiǎn)單的介紹一下PHP吧!PHP 最初是1994年Rasmus Lerdorf創(chuàng)建的,剛剛開始只是一個(gè)簡(jiǎn)單的用Perl語(yǔ)言編寫的程序,用來(lái)統(tǒng)計(jì)他自己網(wǎng)站的訪問者。后來(lái)又用C語(yǔ)言重新編寫,包括可以訪問數(shù)據(jù)庫(kù)。

#T#以后越來(lái)越多的網(wǎng)站使用了PHP,并且強(qiáng)烈要求增加一些特性,比如循環(huán)語(yǔ)句和數(shù)組變量等等,在新的成員加入開發(fā)行列之后,在1995年 中,PHP2.0發(fā)布了。第二版定名為PHP/FI(Form Interpreter)。PHP/FI加入了對(duì)mSQL的支持,從此建立了PHP在動(dòng)態(tài)網(wǎng)頁(yè)開發(fā)上的地位。到了1996年底,有15000個(gè)網(wǎng)站使用 PHP/FI;時(shí)間到了1997年中,使用PHP/FI的網(wǎng)站數(shù)字超過(guò)五萬(wàn)個(gè)。而在1997年中,開始了第三版的開發(fā)計(jì)劃,開發(fā)小組加入了 Zeev Suraski 及 Andi Gutmans,而第三版就定名為PHP3。2000年,PHP4.0又問世了,其中增加了許多新的特性。以下給大家介紹一個(gè)PHP上傳多個(gè)文件的方法。

PHP上傳多個(gè)文件代碼實(shí)現(xiàn):

  1. <?php 
  2. require_once("include/upload.class.php");  
  3. if($_POST["button"])  
  4. {  
  5. //print_r($_FILES);  
  6. //多個(gè)上傳  
  7. //$upload=newTTRUpload($_FILES,"ANY");//同下  
  8.  
  9. $upload=newTTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");  
  10.  
  11. //單個(gè)上傳  
  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;//單個(gè)文件為文件名,批量文件為xxxx|xxxx格式,請(qǐng)注意  
  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. //構(gòu)造函數(shù),$type:ONE單個(gè)上傳ANY批量上傳;  
  53. publicfunction__construct($file,$type="ONE")  
  54. {  
  55. if($type!="ONE"&&$type!="ANY")  
  56. {  
  57. echo"<scriptlanguagescriptlanguage='javascript'>alert('初始化請(qǐng)選擇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"上傳成功!";  
責(zé)任編輯:田樹 來(lái)源: 中國(guó)web第一站
相關(guān)推薦

2009-11-16 10:49:43

PHP上傳文件代碼

2009-11-16 10:40:02

PHP上傳文件代碼

2009-11-16 11:30:55

PHP上傳文件代碼

2009-11-16 14:38:36

PHP上傳文件代碼

2009-11-16 14:15:51

PHP上傳多個(gè)文件

2009-11-16 10:57:51

PHP上傳文件代碼

2009-11-24 14:52:45

PHP動(dòng)態(tài)多文件上傳

2009-11-16 13:04:04

PHP上傳文件代碼

2009-11-27 15:13:00

PHP靜態(tài)變量stat

2010-05-31 14:59:36

PHP + MySQL

2009-11-16 09:35:42

PHP上傳

2009-11-16 11:41:19

PHP上傳大文件

2009-11-16 13:18:10

PHP上傳圖片代碼

2022-09-08 09:39:03

PythonOCR代碼

2009-11-16 10:25:40

PHP上傳文件

2009-11-24 13:15:35

Zend框架PHP上傳文件

2009-11-16 09:26:35

PHP上傳

2009-11-16 10:16:24

PHP文件上傳

2009-11-18 10:22:14

PHP substr

2012-03-14 13:00:44

Play FramewJava
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)