首页 >后端开发 >php教程 >揭秘PHP上传图片代码详细分析_PHP教程

揭秘PHP上传图片代码详细分析_PHP教程

WBOY
WBOY原创
2016-07-15 13:27:09807浏览

学习PHP时,你可能会遇到PHP上传图片问题,这里将介绍详细的PHP上传图片代码解决这个问题,在这里拿出来和大家分享一下。php的语法非常简单,正是它的简单性,使它成为了当前互联网第一编程语言。你不需要具备很多的知识就能上手,比如:你学习C语言,就必须非常了解各个变量如何定义,指针如何操作,内存如何创建销毁等等。

再比如:你学习Java语言,就必须具有面向对象(OO)的基础,就必须清楚是什么时候需要封装,什么时候需要继承,什么时候需要多态,要做项目,怎么还得懂点SSH。PHP的大部分使用者可能根本就没这么多讲究,有的人喜欢面向过程,那你就用面向过程的方式来写代码;有的人喜欢面向对象,那你就用面向对象的方式写代码。Php的产生缘于互联网,目前也是互联网Web2.0第一编程语言。满足用户需求永远是第一位的,可维护性暂且可以放在第二位。我们通常说Web应用永远是β版的,计划远没有变化快。

下面是完整的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上传图片代码,希望对大家有帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446552.htmlTechArticle学习PHP时,你可能会遇到PHP上传图片问题,这里将介绍详细的PHP上传图片代码解决这个问题,在这里拿出来和大家分享一下。php的语法非常...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn