忆困血馆闻 发表于 10-21 21:11:06

[PHP]进阶教程之文件上传篇

本篇文件目录
./upload
./upload.php
./form.html
1.创建一个文件上传表单
纯文本查看 复制代码<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里,有这些代码
纯文本查看 复制代码<?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$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$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]
查看完整版本: [PHP]进阶教程之文件上传篇