Home >Backend Development >PHP Tutorial >Complete code demonstration PHP upload multiple files_PHP tutorial

Complete code demonstration PHP upload multiple files_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-15 13:27:55958browse

I read a book about PHP a few days ago, which impressed me deeply. I will first introduce the development history of PHP, and then teach you a little trick on uploading multiple files using PHP. Let us first briefly introduce PHP! PHP was originally created by Rasmus Lerdorf in 1994. It started as a simple program written in Perl language to count visitors to his own website. Later it was rewritten in C language, including access to the database.

In the future, more and more websites are using PHP, and there is a strong demand to add some features, such as loop statements and array variables, etc., when new members join After the development process, in mid-1995, PHP 2.0 was released. The second version is named PHP/FI (Form Interpreter). PHP/FI added support for mSQL, and has since established PHP's position in dynamic web development. By the end of 1996, there were 15,000 websites using PHP/FI; by mid-1997, the number of websites using PHP/FI exceeded 50,000. In mid-1997, the development plan for the third version was started. The development team joined Zeev Suraski and Andi Gutmans, and the third version was named PHP3. In 2000, PHP4.0 came out again, which added many new features. characteristic. The following will introduce you to a method of uploading multiple files in PHP.

PHP code implementation for uploading multiple files:

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>require_once("include/upload.class.php");  </SPAN><LI class=alt><SPAN>if($_POST["button"])  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>//print_r($_FILES);  </SPAN><LI class=""><SPAN>//多个上传  </SPAN><LI class=alt><SPAN>//$</SPAN><SPAN class=attribute><FONT color=#ff0000>upload</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>newTTRUpload</FONT></SPAN><SPAN>($_FILES,"ANY");//同下  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>upload</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>newTTRUpload</FONT></SPAN><SPAN>(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//单个上传  </SPAN><LI class=""><SPAN>//$</SPAN><SPAN class=attribute><FONT color=#ff0000>upload</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>newTTRUpload</FONT></SPAN><SPAN>($_FILES["file1"]);  </SPAN></SPAN><LI class=alt><SPAN>$upload-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>upload();  </span>
</li>
<li class="">
<span>echo$upload-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getUploadFileName();  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
<li class="alt">
<span><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>htmlxmlns</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>htmlxmlns</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"http://www.w3.org/1999/xhtml"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>metahttp-equiv</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>metahttp-equiv</FONT></STRONG></SPAN><SPAN>="Content-Type"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>content</FONT></SPAN><SPAN>="text/html;</SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>utf</FONT></SPAN><SPAN>-8"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>UntitledDocument</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt"><span> </span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formaction</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formaction</FONT></STRONG></SPAN><SPAN>=""</SPAN><SPAN class=attribute-value><FONT color=#0000ff>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute><FONT color=#ff0000>enctype</FONT></SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"form1"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="file1"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file1"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="file2"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file2"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="file3"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file3"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="file4"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file4"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>/></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="submit"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="button"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>="button"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>value</FONT></SPAN><SPAN>="Submit"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>classTTRUploadextendsError  </SPAN><LI class=alt><SPAN>{  </SPAN><LI class=""><SPAN></SPAN><SPAN class=attribute><FONT color=#ff0000>constfilesize</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>81200000</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>uploadpath</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"uploadfile/"</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>savepath</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>uploadfilename</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>;//单个文件为文件名,批量文件为xxxx|xxxx格式,请注意  </SPAN></SPAN><LI class=""><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>ext</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>("jpg","gif","png");  </SPAN></SPAN><LI class=alt><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>file</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>uploadtype</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>private$</SPAN><SPAN class=attribute><FONT color=#ff0000>filename</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//构造函数,$type:ONE单个上传ANY批量上传;  </SPAN><LI class=alt><SPAN>publicfunction__construct($file,$</SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"ONE"</FONT></SPAN><SPAN>)  </SPAN></SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>if($type!="ONE"&&$type!="ANY")  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>echo"</SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>scriptlanguage</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>scriptlanguage</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'javascript'</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>alert('初始化请选择ONE或者ANY')</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class=""><span>exit;  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadtype</font></span><span>=$type;  </span>
</li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">file</font></span><span>=$file;  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>privatefunctioncreateFileName()  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>return$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">filename</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"TTR_"</font></span><span>.time().$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getRandomN(4);  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>privatefunctiongetUploadPath()  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>if(substr($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>uploadpath,-1,1)!="/")  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">savepath</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>uploadpath."/".date("Ym");  </span>
</li>
<li class=""><span>}else{  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">savepath</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>uploadpath.date("Ym");  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">savepath</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getFolder($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>savepath);  </span>
</li>
<li class=""><span>returntrue;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>privatefunctiongetFileExt($tempfilename)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>returnend(explode(".",$tempfilename));  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>privatefunctiongetExt()  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>if(in_array(strtolower($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getFileExt($tempfilename)),$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>ext))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>returntrue;  </span></li>
<li class="alt"><span>}else{  </span></li>
<li class=""><span>returnfalse;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>privatefunctiongetFolder($folder)  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>if(!is_dir($folder))  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>mkdir($folder);  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>return$folder."/";  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>publicfunctionupload()  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>if($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadtype</font></span><span>=="ONE")  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>if($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getExt($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file["type"]))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>parent::errorExt();  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>}elseif($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file["size"]</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>self::filesize){  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>parent::errorFileSize();  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>}elseif(!$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getUploadPath()){  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>parent::errorUploadPath();  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}else{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">filenametemp</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>createFileName();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">filename</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>savepath.$filenametemp.".".$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getFileExt($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file["name"]);  </span>
</li>
<li class="">
<span>if(move_uploaded_file($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file["tmp_name"],$filename))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadfilename</font></span><span>=$filenametemp;  </span>
</li>
<li class="alt"><span>parent::okMoved();  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>parent::errorMoveUpload();  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>}elseif($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadtype</font></span><span>=="ANY"){  </span>
</li>
<li class="alt"><span> </span></li>
<li class="">
<span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>count</SPAN></FONT></STRONG><SPAN>($this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>file);$i++)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>if($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getExt($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file[$i]["type"]))  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>parent::errorExt();  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>}elseif($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file[$i]["size"]</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>self::filesize){  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>parent::errorFileSize();  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>}elseif(!$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getUploadPath()){  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>parent::errorUploadPath();  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}else{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">filenametemp</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>createFileName();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">filename</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>savepath.$filenametemp.".".$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getFileExt($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file[$i]["name"]);  </span>
</li>
<li class="">
<span>if(move_uploaded_file($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file[$i]["tmp_name"],$filename))  </span>
</li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">str.</font></span><span>=$filenametemp."|";  </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>}else{  </span></li>
<li class="alt"><span>parent::errorMoveUpload();  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadfilename</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">substr</font></span><span>($str,0,strlen($str)-1);  </span>
</li>
<li class=""><span>parent::okMoved();  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>publicfunctiongetUploadFileName()  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>return$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>uploadfilename;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>publicfunctionsetUploadPath($path)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadpath</font></span><span>=$path;  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>privatefunctiongetRandomN($n)  </span></li>
<li class=""><span>{  </span></li>
<li class="alt">
<span>if($n</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>1</SPAN></FONT></STRONG><SPAN>||$n</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>10)return"";  </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ary_num</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(0,1,2,3,4,5,6,7,8,9);  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">return</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>;  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>$n;$i++)  </SPAN></SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute-value><FONT color=#0000ff>rand</FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>randn</FONT></SPAN><SPAN>=rand(0,9-$i);  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>return.</FONT></SPAN><SPAN>=$ary_num[$randn];  </SPAN></SPAN><LI class=alt><SPAN>$ary_num[$randn]=$ary_num[9-$i];  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN>return$return;  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>publicfunction__destruct()  </SPAN><LI class=alt><SPAN>{  </SPAN><LI class=""><SPAN>$this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span class="attribute"><font color="#ff0000">uploadfilename</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">uploadtype</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">file</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="alt">
<span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">savepath</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>classError  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>publicstaticfunctionerrorFileSize()  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>echo"超出最大上传限制";  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>publicstaticfunctionerrorExt()  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>echo"此类文件不允许上传";  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>publicstaticfunctionerrorUploadPath()  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>echo"上传路径不正确";  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>publicstaticfunctionerrorMoveUpload()  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>echo"上传失败";  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>publicstaticfunctionokMoved()  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>echo"上传成功!";  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>publicstaticfunctionokArrayMoved()  </span></li>
<li class=""><span>{  </span></li>
<li class="alt"><span>echo"上传成功!";  </span></li>
<li class=""><span>} </span></li>
</ol>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446489.htmlTechArticleI read a book about PHP a few days ago, which touched me deeply. Let me introduce PHP first. Development history, and then teach you a little trick on how to upload multiple files with PHP. Let's keep it simple first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn