Revealing the Detailed Analysis of PHP Image Upload Code_PHP Tutorial

WBOY
Release: 2016-07-15 13:27:09
Original
770 people have browsed it

When learning PHP, you may encounter the problem of uploading images in PHP. Here we will introduce the detailed PHP image upload code to solve this problem. I will share it with you here. The syntax of PHP is very simple. It is its simplicity that makes it the number one programming language on the Internet. You don't need to have a lot of knowledge to get started. For example, if you learn C language, you must have a good understanding of how each variable is defined, how pointers are operated, how memory is created and destroyed, etc.

Another example: when you learn Java language, you must have the foundation of object-oriented (OO), and you must know when encapsulation is needed and when inheritance is needed. , when do you need polymorphism? Why do you need to know SSH when you want to do a project? Most users of PHP may not be so particular at all. Some people like process-oriented, so you should write code in a process-oriented way; some people like object-oriented, then you should write code in an object-oriented way. . Php originated from the Internet, and it is currently the first programming language of Web2.0 on the Internet. Meeting user needs always comes first, and maintainability can be placed second for the time being. We usually say that web applications will always be beta versions, and plans are far from changing as fast.

The following is the complete PHP image upload code;

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</SPAN><SPAN class=attribute>formenctype</SPAN><SPAN class=tag-name>formenctype</SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value>action</SPAN><SPAN>=""</SPAN><SPAN class=attribute>method</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"post"</SPAN><SPAN class=tag>></span><span> </span></span></li>
<li class="">
<span></span><span class="tag"><</SPAN><SPAN class=attribute>inputtype</SPAN><SPAN class=tag-name>inputtype</SPAN><SPAN>="hidden"</SPAN><SPAN class=attribute-value>name</SPAN><SPAN>="max_file_size"</SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"200000"</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class="alt">
<span></span><span class="tag"><</SPAN><SPAN class=attribute>inputname</SPAN><SPAN class=tag-name>inputname</SPAN><SPAN>="userfile"</SPAN><SPAN class=attribute-value>type</SPAN><SPAN>="file"</SPAN><SPAN class=tag>></span><span>  </span>
</li>
<li class="">
<span></span><span class="tag"><</SPAN><SPAN class=attribute>inputtype</SPAN><SPAN class=tag-name>inputtype</SPAN><SPAN>="submit"</SPAN><SPAN class=attribute-value>name</SPAN><SPAN>="submit"</SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"上传文件"</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class="alt">
<span></span><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span><span> </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>array</SPAN><SPAN>("jpg","gif","bmp","jpeg","png");  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>uploaddir</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"./file/"</SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>if(isset($_POST['submit']))  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>functiontexttype($name)  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>returnsubstr(strrchr($name,'.'),1);  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN>if(!in_array(strtolower(texttype($_FILES['userfile']['name'])),$type))  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>text</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>implode</SPAN><SPAN>(",",$type);  </SPAN></SPAN><LI class=""><SPAN>echo"您只能上传以下类型文件:",$text,"</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span>else  </span></li>
<li class="alt"><span>{  </span></li>
<li class="">
<span>if($_FILES['userfile']['size']</span><span class="tag"><</SPAN><SPAN>="200000")  </SPAN></SPAN><LI class=alt><SPAN>{  </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>filename</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>explode</SPAN><SPAN>(".",$_FILES['userfile']['name']);  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>time</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>date</SPAN><SPAN>("m-d-H-i-s");  </SPAN></SPAN><LI class=""><SPAN>$filename[0]=$time;  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>name</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>implode</SPAN><SPAN>(".",$filename);  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>uploadfile</SPAN><SPAN>=$uploaddir.$name;  </SPAN></SPAN><LI class=alt><SPAN>if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile))  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>echo"</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span>您的文件已经上传完毕上传图片预览:</span><span class="tag"></</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>imgsrc</SPAN><SPAN class=tag-name>imgsrc</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>'$uploadfile'</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span>";  </span>
</li>
<li class="">
<span>echo"</span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>ahref</SPAN><SPAN class=tag-name>ahref</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>'javascrīpt:history.go(-1)'</SPAN><SPAN class=tag>></span><span>继续上传</span><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span>else  </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>else  </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>}  </span></li>
<li class="alt">
<span></span><span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

The above is the detailed PHP image upload code, I hope it will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446552.htmlTechArticleWhen learning PHP, you may encounter problems with PHP uploading images. Here we will introduce the detailed PHP image uploading code solution I would like to share this question with you here. The syntax of php is very...
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