经验总结:示例PHP上传文件代码_PHP教程

WBOY
发布: 2016-07-15 13:27:00
原创
691 人浏览过

今天看了一本关于PHP的书,让我了解了PHP上传文件的方法,最主要的两个函数是move_uploade_file(临时文件,目标位置和文件名)和is_uploaded_file(),前者用来移动上传后保存在服务器缓存区的文件到目标文件,后者用来判断文件是否上传成功。除了以上两个函数之外,还要说明一下form标签中enctype的值应该如下:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">formenctype</font></span><span class="tag-name"><strong><font color="#006699">formenctype</font></strong></span><span>="multipart/form-data"</span><span class="attribute-value"><font color="#0000ff">method</font></span><span>="post"</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"upform"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span></span></li></ol>
登录后复制

只有其值为multipart/form-data才能保证以正确的编码方式上传文件。input标签type属性中的"file"

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">inputname</font></span><span class="tag-name"><strong><font color="#006699">inputname</font></strong></span><span>="upfile"</span><span class="attribute-value"><font color="#0000ff">type</font></span><span>="file"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span></span></li></ol>
登录后复制

另一个系统函数是$_FILES,$_FILES['myFile']['name']客户端文件的原名称、$_FILES['myFile']['type']文件的MIME类型,例如"image/gif"、$_FILES['myFile']['size']已上传文件的大小,单位为字节、$_FILES['myFile']['tmp_name']储存的临时文件名,一般是系统默认、$_FILES['myFile']['error']该文件上传相关的错误代码。这个函数将上传文件的信息分割成数组形式保存在不同的数组元素中,例如,文件名的值存储在$_FILES['myFile']['name']中。下面附上自己写的简单的PHP上传文件代码:

PHP上传文件代码类saveupload.php

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>if(is_uploaded_file($_FILES['upfile']['tmp_name'])){  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">upfile</font></span><span>=$_FILES["upfile"];//如果已经选定了要上传的文件,将其索引保存在$upfile中  </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="attribute"><font color="#ff0000">name</font></span><span>=$upfile["name"];  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=$upfile["type"];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">size</font></span><span>=$upfile["size"];  </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">tmp_name</font></span><span>=$upfile["tmp_name"];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">error</font></span><span>=$upfile["error"];  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//设定上传文件类型  </span></li>
<li class="alt"><span>switch($type){  </span></li>
<li class=""><span>case'image/pjpeg':  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ok</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">1</font></span><span>;  </span>
</li>
<li class=""><span>break;  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>case'image/jpeg':  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ok</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">1</font></span><span>;  </span>
</li>
<li class=""><span>break;  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>case'image/png':  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ok</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">1</font></span><span>;  </span>
</li>
<li class=""><span>break;  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>case'image/gif':  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">ok</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">1</font></span><span>;  </span>
</li>
<li class=""><span>break;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//如果文件类型合法并且$error返回值为0,说明上传成功  </span></li>
<li class="alt">
<span>if($ok&&$</span><span class="attribute"><font color="#ff0000">error</font></span><span>=='0'){  </span>
</li>
<li class=""><span>move_uploaded_file($tmp_name,'up/'.$name);//将保存在缓存的文件移动到指定目录下  </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"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
登录后复制

PHP上传文件代码上传页面upload.php

<ol class="dp-xml">
<li class="alt"><span><span>-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">htmlxmlns</font></span><span class="tag-name"><strong><font color="#006699">htmlxmlns</font></strong></span><span>=</span><span class="attribute-value"><font color="#0000ff">"http://www.w3.org/1999/xhtml"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">metahttp-equiv</font></span><span class="tag-name"><strong><font color="#006699">metahttp-equiv</font></strong></span><span>="Content-Type"</span><span class="attribute-value"><font color="#0000ff">content</font></span><span>="text/html;</span><span class="attribute"><font color="#ff0000">charset</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">utf</font></span><span>-8"</span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>upload</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">styletype</font></span><span class="tag-name"><strong><font color="#006699">styletype</font></strong></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text/css"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span> </span>
</li>
<li class="alt"><span></span></li>
</ol>
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板