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.
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>
The above is the detailed PHP image upload code, I hope it will be helpful to everyone.