Experience summary: Sample PHP file upload code_PHP tutorial

WBOY
Release: 2016-07-15 13:27:00
Original
693 people have browsed it

I read a book about PHP today, which taught me how to upload files in PHP. The two most important functions are move_uploade_file (temporary file, target location and file name) and is_uploaded_file(), the former It is used to move files saved in the server cache area after uploading to the target file. The latter is used to determine whether the file is uploaded successfully. In addition to the above two functions, it is also necessary to explain that the value of enctype in the form tag should be as follows:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formenctype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formenctype</FONT></STRONG></SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upform"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li></ol>
Copy after login

Only when its value is multipart/form-data can the file be uploaded in the correct encoding method. "file" in the input tag type attribute

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputname</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputname</FONT></STRONG></SPAN><SPAN>="upfile"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>type</FONT></SPAN><SPAN>="file"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li></ol>
Copy after login

Another system function is $_FILES, $_FILES['myFile']['name'] The original name of the client file, the MIME type of the $_FILES['myFile']['type'] file, such as "image/gif", $_FILES['myFile']['size'] the size of the uploaded file, unit Temporary file name stored in bytes, $_FILES['myFile']['tmp_name'], usually the system default, $_FILES['myFile']['error'] error code related to the file upload. This function divides the uploaded file information into an array and stores it in different array elements. For example, the value of the file name is stored in $_FILES['myFile']['name']. Attached below is the simple PHP file upload code I wrote:

PHP file upload code class saveupload.php

<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>if(is_uploaded_file($_FILES['upfile']['tmp_name'])){  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>upfile</FONT></SPAN><SPAN>=$_FILES["upfile"];//如果已经选定了要上传的文件,将其索引保存在$upfile中  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//分别去上传文件的名字,类型等  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=$upfile["name"];  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=$upfile["type"];  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>size</FONT></SPAN><SPAN>=$upfile["size"];  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tmp_name</FONT></SPAN><SPAN>=$upfile["tmp_name"];  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=$upfile["error"];  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//设定上传文件类型  </SPAN><LI class=alt><SPAN>switch($type){  </SPAN><LI class=""><SPAN>case'image/pjpeg':  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>break;  </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/jpeg':  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>break;  </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/png':  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>break;  </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/gif':  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>break;  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//如果文件类型合法并且$error返回值为0,说明上传成功  </SPAN><LI class=alt><SPAN>if($ok&&$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=='0'){  </SPAN></SPAN><LI class=""><SPAN>move_uploaded_file($tmp_name,'up/'.$name);//将保存在缓存的文件移动到指定目录下  </SPAN><LI class=alt><SPAN>echo"上传成功";  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

PHP file upload code upload Page upload.php

<ol class="dp-xml">
<li class="alt"><span><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></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>upload</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><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>styletype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>styletype</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"text/css"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span>
</li>
<li class="alt">
<span><!--  </SPAN><LI class=""><SPAN>body{  </SPAN><LI class=alt><SPAN>background-color:#CFF;  </SPAN><LI class=""><SPAN>text-align:center;  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN>--</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>style</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>head</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>body</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>hr</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>formid</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formid</FONT></STRONG></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>action</FONT></SPAN><SPAN>="saveupload.php"</SPAN><SPAN class=attribute><FONT color=#ff0000>enctype</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"multipart/form-data"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><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>label</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>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>="upfile"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>label</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>label</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>="上传"</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>label</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>form</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>
</ol>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446556.htmlTechArticleI read a book about PHP today, which taught me how to upload files in PHP. The two most important things are: The two functions are move_uploade_file (temporary file, target location and file name) and is_uploaded_fi...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template