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

WBOY
Freigeben: 2016-07-15 13:27:00
Original
692 Leute haben es durchsucht

今天看了一本关于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>
Nach dem Login kopieren

只有其值为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>
Nach dem Login kopieren

另一个系统函数是$_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>
Nach dem Login kopieren

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>
Nach dem Login kopieren
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage