Home > Article > Backend Development > Study notes: Detailed explanation of PHP image upload code_PHP tutorial
Want to know how to write the code for uploading images? Let me analyze it in detail with you. With PHP, you always have multiple ways to accomplish a specific task. Let’s take file upload as an example. Of course, you can use HTTP file upload in the traditional way, transferring files directly to the web server disk.
PHP upload image code:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>phpsession_start</SPAN><SPAN>();</SPAN><SPAN class=tag>?></span><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>id</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>mysql_connect</SPAN><SPAN>('localhost','root','585858'); </SPAN></SPAN><LI class=alt><SPAN>mysql_select_db("okhwyy",$id); </SPAN><LI class=""><SPAN>mysql_query("setnamesgb2312"); </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag>?></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>metahttp-equiv</SPAN><SPAN class=tag-name>metahttp-equiv</SPAN><SPAN>="Content-Type"</SPAN><SPAN class=attribute-value>c</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span><span>限制上传图片的格式</span><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>styletype</SPAN><SPAN class=tag-name>styletype</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"text/css"</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span><!-- </SPAN><LI class=""><SPAN>.style1{ </SPAN><LI class=alt><SPAN>font-size:14px; </SPAN><LI class=""><SPAN>font-family:"华文行楷"; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>.style4{font-size:12px;font-weight:bold;} </SPAN><LI class=alt><SPAN>--</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>style</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tablewidth</SPAN><SPAN class=tag-name>tablewidth</SPAN><SPAN>="406"</SPAN><SPAN class=attribute-value>height</SPAN><SPAN>="129"</SPAN><SPAN class=attribute>border</SPAN><SPAN>="0"</SPAN><SPAN class=attribute-value>align</SPAN><SPAN>="center"</SPAN><SPAN class=attribute>cellpadding</SPAN><SPAN>="0"</SPAN><SPAN class=attribute-value>cellspacing</SPAN><SPAN>="0"</SPAN><SPAN class=attribute>background</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>""</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdwidth</SPAN><SPAN class=tag-name>tdwidth</SPAN><SPAN>="106"</SPAN><SPAN class=attribute-value>height</SPAN><SPAN>="40"</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdwidth</SPAN><SPAN class=tag-name>tdwidth</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"196"</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdwidth</SPAN><SPAN class=tag-name>tdwidth</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"31"</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>formname</SPAN><SPAN class=tag-name>formname</SPAN><SPAN>="form1"</SPAN><SPAN class=attribute-value>method</SPAN><SPAN>="post"</SPAN><SPAN class=attribute>action</SPAN><SPAN>=""</SPAN><SPAN class=attribute-value>enctype</SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdheight</SPAN><SPAN class=tag-name>tdheight</SPAN><SPAN>="32"</SPAN><SPAN class=attribute-value>align</SPAN><SPAN>="right"</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>spanclass</SPAN><SPAN class=tag-name>spanclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"style1"</SPAN><SPAN class=tag>></span><span>图片路径</span><span class="tag"></</SPAN><SPAN class=tag-name>span</SPAN><SPAN class=tag>></span><span>:</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdvalign</SPAN><SPAN class=tag-name>tdvalign</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"middle"</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>inputname</SPAN><SPAN class=tag-name>inputname</SPAN><SPAN>="images"</SPAN><SPAN class=attribute-value>type</SPAN><SPAN>="file"</SPAN><SPAN class=attribute>id</SPAN><SPAN>="images2"</SPAN><SPAN class=attribute-value>size</SPAN><SPAN>="15"</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <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>"30000"</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdheight</SPAN><SPAN class=tag-name>tdheight</SPAN><SPAN>="44"</SPAN><SPAN class=attribute-value>align</SPAN><SPAN>="right"</SPAN><SPAN class=attribute>valign</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"middle"</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>spanclass</SPAN><SPAN class=tag-name>spanclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"style4"</SPAN><SPAN class=tag>></span><span>图片的格式</span><span class="tag"></</SPAN><SPAN class=tag-name>span</SPAN><SPAN class=tag>></span><span>:</span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdvalign</SPAN><SPAN class=tag-name>tdvalign</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"middle"</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>spanclass</SPAN><SPAN class=tag-name>spanclass</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"style4"</SPAN><SPAN class=tag>></span><span>(.jpg)</span><span class="tag"></</SPAN><SPAN class=tag-name>span</SPAN><SPAN class=tag>></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 class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</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><span class="tag"><</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdheight</SPAN><SPAN class=tag-name>tdheight</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"10"</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>table</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tablewidth</SPAN><SPAN class=tag-name>tablewidth</SPAN><SPAN>="406"</SPAN><SPAN class=attribute-value>height</SPAN><SPAN>="129"</SPAN><SPAN class=attribute>border</SPAN><SPAN>="1"</SPAN><SPAN class=attribute-value>align</SPAN><SPAN>="center"</SPAN><SPAN class=attribute>cellpadding</SPAN><SPAN>="0"</SPAN><SPAN class=attribute-value>cellspacing</SPAN><SPAN>="0"</SPAN><SPAN class=tag>></span><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>query</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"select*fromtb_image2whereidorderbydatadesclimit2"</SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>result</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>mysql_query</SPAN><SPAN>($query); </SPAN></SPAN><LI class=""><SPAN>if($result){ </SPAN><LI class=alt><SPAN>while($</SPAN><SPAN class=attribute>row</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>mysql_fetch_array</SPAN><SPAN>($result)){ </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag>?></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdwidth</SPAN><SPAN class=tag-name>tdwidth</SPAN><SPAN>="106"</SPAN><SPAN class=attribute-value>align</SPAN><SPAN>="center"</SPAN><SPAN class=tag>></span><span class="tag"><?</SPAN><SPAN class=tag-name>phpecho</SPAN><SPAN>$row[data];</SPAN><SPAN class=tag>?></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>tdwidth</SPAN><SPAN class=tag-name>tdwidth</SPAN><SPAN>="196"</SPAN><SPAN class=attribute-value>align</SPAN><SPAN>="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=tag><?</SPAN><SPAN class=tag-name>phpecho</SPAN><SPAN>$row[path];</SPAN><SPAN class=tag>?></span><span>"</span><span class="attribute-value">width</span><span>="200"</span><span class="attribute">height</span><span>=</span><span class="attribute-value">"120"</span><span class="tag">></span><span class="tag"></</SPAN><SPAN class=tag-name>td</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>tr</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN>}}</SPAN><SPAN class=tag>?></span><span> </span> </li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>table</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"><span> </span></li> <li class=""> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>html</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>Submit</SPAN><SPAN>=$_POST[Submit]; </SPAN></SPAN><LI class=alt><SPAN>if($Submit){ </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>image</SPAN><SPAN>=$_FILES['images']['name']; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>date</SPAN><SPAN class=attribute-value>date</SPAN><SPAN>=date("Y-m-d"); </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>path</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"upfiles/"</SPAN><SPAN>.$_FILES['images']['name']; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>strstr</SPAN><SPAN>($path,"."); </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>size</SPAN><SPAN>=$_FILES['images']['size']; </SPAN></SPAN><LI class=alt><SPAN>if($size</SPAN><SPAN class=tag>></span><span>1000000){echo"</span><span class="tag"><</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span><span>alert('上传容量超限');history.back();</span><span class="tag"></</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span><span>";} </span> </li> <li class=""> <span>elseif($type!=".jpg"){echo"</span><span class="tag"><</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span><span>alert('上传类型不对');history.back();</span><span class="tag"></</SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></span><span>";} </span> </li> <li class="alt"><span>elseif(move_uploaded_file($_FILES['images']['tmp_name'],$path)){ </span></li> <li class=""> <span>$</span><span class="attribute">query</span><span>=</span><span class="attribute-value">"insertintotb_image2(image_name,path,data)values('$image','$path','$date')"</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">result</span><span>=</span><span class="attribute-value">mysql_query</span><span>($query)ordie(mysql_error()); </span> </li> <li class=""><span>if($result){ </span></li> <li class="alt"><span>echo"上传成功!"; </span></li> <li class=""> <span>echo"</span><span class="tag"><</SPAN><SPAN class=attribute>metahttp-equiv</SPAN><SPAN class=tag-name>metahttp-equiv</SPAN><SPAN>=\"Refresh\"</SPAN><SPAN class=attribute>content</SPAN><SPAN>=\"3;</SPAN><SPAN class=attribute>url</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>index</SPAN><SPAN>.php\"</SPAN><SPAN class=tag>></span><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>else{ </span></li> <li class="alt"><span>echo"上传失败!"; </span></li> <li class=""> <span>echo"</span><span class="tag"><</SPAN><SPAN class=attribute>metahttp-equiv</SPAN><SPAN class=tag-name>metahttp-equiv</SPAN><SPAN>=\"Refresh\"</SPAN><SPAN class=attribute>content</SPAN><SPAN>=\"3;</SPAN><SPAN class=attribute>url</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>index</SPAN><SPAN>.php\"</SPAN><SPAN class=tag>></span><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>