找回密码
 立即注册
搜索

[经验杂志] [PHP]进阶教程之文件上传篇

[复制链接]
发表于 10-21 21:11:06 | 显示全部楼层 |阅读模式
本篇文件目录
./upload
./upload.php
./form.html
1.创建一个文件上传表单
[AppleScript] 纯文本查看 复制代码<html><head><meta charset=&quot;utf-8&quot;><title>haihaihai</title></head><body><form action=&quot;upload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;><label for=&quot;file&quot;>文件名:</label><input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot;><br><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;提交&quot;></form></body></html>
以下代码保存在form.html里


标签说明
    <form> 标签的 enctype 属性规定了在提交表单时要使用哪种内容类型。在表单需要二进制数据时,比如文件内容,请使用 &quot;multipart/form-data&quot;。<input> 标签的 type=&quot;file&quot; 属性规定了应该把输入作为文件来处理。举例来说,当在浏览器中预览时,会看到输入框旁边有一个浏览按钮。





在upload.php里,有这些代码
[PHP] 纯文本查看 复制代码<?phpif ($_FILES[&quot;file&quot;][&quot;error&quot;] > 0){echo &quot;错误:&quot; . $_FILES[&quot;file&quot;][&quot;error&quot;] . &quot;<br>&quot;;}else{echo &quot;上传文件名: &quot; . $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot;<br>&quot;;echo &quot;文件类型: &quot; . $_FILES[&quot;file&quot;][&quot;type&quot;] . &quot;<br>&quot;;echo &quot;文件大小: &quot; . ($_FILES[&quot;file&quot;][&quot;size&quot;] / 1024) . &quot; kB<br>&quot;;echo &quot;文件临时存储的位置: &quot; . $_FILES[&quot;file&quot;][&quot;tmp_name&quot;];}?>
怎么添加限制呢?
[PHP] 纯文本查看 复制代码<?php$allowedExts = array(&quot;gif&quot;, &quot;jpeg&quot;, &quot;jpg&quot;, &quot;png&quot;);$temp = explode(&quot;.&quot;, $_FILES[&quot;file&quot;][&quot;name&quot;]);$extension = end($temp);        // 获取文件后缀名if ((($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/gif&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/jpeg&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/jpg&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/pjpeg&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/x-png&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/png&quot;))&& ($_FILES[&quot;file&quot;][&quot;size&quot;] < 204800)    // 小于 200 kb&& in_array($extension, $allowedExts)){if ($_FILES[&quot;file&quot;][&quot;error&quot;] > 0){echo &quot;错误:: &quot; . $_FILES[&quot;file&quot;][&quot;error&quot;] . &quot;<br>&quot;;}else{echo &quot;上传文件名: &quot; . $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot;<br>&quot;;echo &quot;文件类型: &quot; . $_FILES[&quot;file&quot;][&quot;type&quot;] . &quot;<br>&quot;;echo &quot;文件大小: &quot; . ($_FILES[&quot;file&quot;][&quot;size&quot;] / 1024) . &quot; kB<br>&quot;;echo &quot;文件临时存储的位置: &quot; . $_FILES[&quot;file&quot;][&quot;tmp_name&quot;];}}else{echo &quot;文件超过限制&quot;;}?>
上面的文件在服务器的 PHP 临时文件夹中创建了一个被上传文件的临时副本。这个临时的副本文件会在脚本结束时消失。要保存被上传的文件,我们需要把它拷贝到另外的位置:[PHP] 纯文本查看 复制代码<?php$allowedExts = array(&quot;gif&quot;, &quot;jpeg&quot;, &quot;jpg&quot;, &quot;png&quot;);$temp = explode(&quot;.&quot;, $_FILES[&quot;file&quot;][&quot;name&quot;]);echo $_FILES[&quot;file&quot;][&quot;size&quot;];$extension = end($temp);     // 获取文件后缀名if ((($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/gif&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/jpeg&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/jpg&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/pjpeg&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/x-png&quot;)|| ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/png&quot;))&& ($_FILES[&quot;file&quot;][&quot;size&quot;] < 204800)   // 小于 200 kb&& in_array($extension, $allowedExts)){if ($_FILES[&quot;file&quot;][&quot;error&quot;] > 0){echo &quot;错误:: &quot; . $_FILES[&quot;file&quot;][&quot;error&quot;] . &quot;<br>&quot;;}else{echo &quot;上传文件名: &quot; . $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot;<br>&quot;;echo &quot;文件类型: &quot; . $_FILES[&quot;file&quot;][&quot;type&quot;] . &quot;<br>&quot;;echo &quot;文件大小: &quot; . ($_FILES[&quot;file&quot;][&quot;size&quot;] / 1024) . &quot; kB<br>&quot;;echo &quot;文件临时存储的位置: &quot; . $_FILES[&quot;file&quot;][&quot;tmp_name&quot;] . &quot;<br>&quot;;// 判断当期目录下的 upload 目录是否存在该文件// 如果没有 upload 目录,你需要创建它,upload 目录权限为 777if (file_exists(&quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;])){echo $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot; 文件已经存在。 &quot;;}else{// 如果 upload 目录不存在该文件则将文件上传到 upload 目录下move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;], &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]);echo &quot;文件存储在: &quot; . &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;];}}}else{echo &quot;文件超过限制&quot;;}?>完结
声明
1,本帖收录内容来源于系统采集或网友自主提交,不代表本网站立场!
2,本帖收录内容,仅供参考使用,本站不对其安全性,正确性等作出保证。
3,如您认为本帖收录内容侵犯了您的版权或者违规,请在右下角进行举报,发现直接删帖!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|wcbb导航网-属于年轻人的导航网-网站导航-素材导航论坛! ( 桂ICP备2024029389号-2 )|网站地图

快速回复 返回顶部 返回列表