Heim > Artikel > Backend-Entwicklung > 揭秘PHP上传图片代码详细分析_PHP教程
学习PHP时,你可能会遇到PHP上传图片问题,这里将介绍详细的PHP上传图片代码解决这个问题,在这里拿出来和大家分享一下。php的语法非常简单,正是它的简单性,使它成为了当前互联网第一编程语言。你不需要具备很多的知识就能上手,比如:你学习C语言,就必须非常了解各个变量如何定义,指针如何操作,内存如何创建销毁等等。
下面是完整的PHP上传图片代码;
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><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 class=""> <span></span><span class="tag"><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></span> </li> <li class="alt"> <span></span><span class="tag"><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></span> </li> <li class=""> <span></span><span class="tag"><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></span> </li> <li class="alt"> <span></span><span class="tag"></span><span class="tag-name">form</span><span class="tag">></span><span> </span> </li></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="tag"></span><span class="tag-name">php</span><span> </span> </li> <li class=""> <span>$</span><span class="attribute">type</span><span>=</span><span class="attribute-value">array</span><span>("jpg","gif","bmp","jpeg","png"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">uploaddir</span><span>=</span><span class="attribute-value">"./file/"</span><span>; </span> </li> <li class=""><span> </span></li> <li class="alt"><span>if(isset($_POST['submit'])) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>functiontexttype($name) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>returnsubstr(strrchr($name,'.'),1); </span></li> <li class=""><span>} </span></li> <li class="alt"><span>if(!in_array(strtolower(texttype($_FILES['userfile']['name'])),$type)) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">text</span><span>=</span><span class="attribute-value">implode</span><span>(",",$type); </span> </li> <li class=""> <span>echo"您只能上传以下类型文件:",$text,"</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><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>="200000") </span></span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">filename</span><span>=</span><span class="attribute-value">explode</span><span>(".",$_FILES['userfile']['name']); </span> </li> <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> </li> <li class=""><span>$filename[0]=$time; </span></li> <li class="alt"> <span>$</span><span class="attribute">name</span><span>=</span><span class="attribute-value">implode</span><span>(".",$filename); </span> </li> <li class=""> <span>$</span><span class="attribute">uploadfile</span><span>=$uploaddir.$name; </span> </li> <li class="alt"><span>if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>echo"</span><span class="tag"><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 class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">center</span><span class="tag">></span><span class="tag"><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></span><li class=""> <span>echo"</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">center</span><span class="tag">></span><span class="tag"><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></span><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></span></span> </li></span></span></span> </li> </ol>
以上就是详细的PHP上传图片代码,希望对大家有帮助。