实战PHP上传文件代码演示_PHP教程

php中文网
发布: 2016-07-15 13:26:55
原创
812人浏览过

php经过长时间的发展,很多用户都很了解php了,这里我发表一下php上传文件代码,和大家讨论讨论。php本身是一种简单而强大的语言。php语言拥有核心特性如强大的字符串和数组处理能力,同时极大的改进了对面向对象编程的支持(php5以上版本)。通过使用标准的和可选的扩展模块,php应用程序可以连接mysql或oracle等十几种数据库、绘图、创建pdf文件和创建解析xml文件。你也可以使用c语言来写自己的php扩展模块。

例如,在已存在的代码库中提供一个PHP的接口函数。你也可以在Windows下运行PHP,使用COM控制其它诸如Word和Excel的Windows应用程序,或者使用ODBC来连接数据库。在国内,PHP曾经和微软的ASP并驾齐驱,是大家常用的网络编程语言。 ASP3.0现在早已经被微软放弃了,不再更新,也许有人会认为:PHP应该也是个快作古的东西了吧?然而事实恰恰相反,PHP不仅在高速发展,把ASP远远抛在身后,而且渐渐成为现在互联网上使用最为广泛、最热门的语言。

PHP上传文件代码

芦笋演示
芦笋演示

一键出成片的录屏演示软件,专为制作产品演示、教学课程和使用教程而设计。

芦笋演示 34
查看详情 芦笋演示
<OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI class=""><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 class=tag></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN><SPAN>   </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>form</SPAN><SPAN> </SPAN><SPAN class=attribute>enctype</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"multipart/form-data"</SPAN><SPAN> </SPAN><SPAN class=attribute>action</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>""</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=alt><SPAN>请选择文件: </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>input</SPAN><SPAN> </SPAN><SPAN class=attribute>name</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"upload_file"</SPAN><SPAN> </SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"file"</SPAN><SPAN class=tag>></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>input</SPAN><SPAN> </SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"submit"</SPAN><SPAN> </SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"上传文件"</SPAN><SPAN class=tag>></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></SPAN><SPAN>   </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN><SPAN>   </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><?</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>upload_file</SPAN><SPAN>=$_FILES['upload_file']['tmp_name'];  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>upload_file_name</SPAN><SPAN>=$_FILES['upload_file']['name'];  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>if($upload_file){  </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>file_size_max</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>1000</SPAN><SPAN>*1000;// 1M限制文件上传最大容量(bytes)  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>store_dir</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>"d:/"</SPAN><SPAN>;// 上传文件的储存位置  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>accept_overwrite</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>1</SPAN><SPAN>;//是否允许覆盖相同文件  </SPAN></SPAN><LI class=alt><SPAN>// 检查文件大小  </SPAN><LI class=""><SPAN>if ($upload_file_size </SPAN><SPAN class=tag>></SPAN><SPAN> $file_size_max) {  </SPAN></SPAN><LI class=alt><SPAN>echo "对不起,你的文件容量大于规定";  </SPAN><LI class=""><SPAN>exit;  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// 检查读写文件  </SPAN><LI class=""><SPAN>if (file_exists($store_dir . $upload_file_name) && !$accept_overwrite) {  </SPAN><LI class=alt><SPAN>Echo "存在相同文件名的文件";  </SPAN><LI class=""><SPAN>exit;  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//复制文件到指定目录  </SPAN><LI class=""><SPAN>if (!move_uploaded_file($upload_file,$store_dir.$upload_file_name)) {  </SPAN><LI class=alt><SPAN>echo "复制文件失败";  </SPAN><LI class=""><SPAN>exit;  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>Echo "</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>></SPAN><SPAN>你上传了文件:";  </SPAN></SPAN><LI class=""><SPAN>echo$_FILES['upload_file']['name'];  </SPAN><LI class=alt><SPAN>echo "</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>";  </SPAN></SPAN><LI class=""><SPAN>//客户端机器文件的原名称。   </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>Echo "文件的 MIME 类型为:";  </SPAN><LI class=alt><SPAN>echo $_FILES['upload_file']['type'];  </SPAN><LI class=""><SPAN>//文件的 MIME 类型,需要浏览器提供该信息的支持,例如“image/gif”。   </SPAN><LI class=alt><SPAN>echo "</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>";  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>Echo "上传文件大小:";  </SPAN><LI class=""><SPAN>echo $_FILES['upload_file']['size'];  </SPAN><LI class=alt><SPAN>//已上传文件的大小,单位为字节。   </SPAN><LI class=""><SPAN>echo "</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>";  </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>Echo "文件上传后被临时储存为:";  </SPAN><LI class=alt><SPAN>echo $_FILES['upload_file']['tmp_name'];  </SPAN><LI class=""><SPAN>//文件被上传后在服务端储存的临时文件名。   </SPAN><LI class=alt><SPAN>echo "</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></SPAN><SPAN>";  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>Erroe</SPAN><SPAN>=$_FILES['upload_file']['error'];  </SPAN></SPAN><LI class=alt><SPAN>switch($Erroe){  </SPAN><LI class=""><SPAN>case 0:  </SPAN><LI class=alt><SPAN>Echo "上传成功"; break;  </SPAN><LI class=""><SPAN>case 1:  </SPAN><LI class=alt><SPAN>Echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值."; break;  </SPAN><LI class=""><SPAN>case 2:  </SPAN><LI class=alt><SPAN>Echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。";break;  </SPAN><LI class=""><SPAN>case 3:  </SPAN><LI class=alt><SPAN>Echo "文件只有部分被上传";break;  </SPAN><LI class=""><SPAN>case 4:  </SPAN><LI class=alt><SPAN>Echo "没有文件被上传";break;  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag>?></SPAN><SPAN> </SPAN></SPAN></LI></OL>
登录后复制


以上就是详细的PHP上传文件代码,希望对大家有帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446559.htmlTechArticlePHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下PHP上传文件代码,和大家讨论讨论。PHP本身是一种简单而强大的语言。PHP语...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号