Heim >Backend-Entwicklung >PHP-Tutorial >php上传文件常见有关问题(基础)
php上传文件常见问题(基础)
既然上一篇文章《php上传中文文件文件名乱码问题》遇到了文件上传的问题,干脆把php上传文件时经常碰到的几个问题总结一下吧,以后用到时不用再去找了。
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=utf-8"</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="upload_file.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #008080;"> 8</span> <span style="color: #ff0000;">enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;"><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="file"</span><span style="color: #0000ff;">></span>Filename:<span style="color: #0000ff;"></span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #008080;">10</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="file"</span> <span style="color: #0000ff;">/></span> <span style="color: #008080;">11</span> <span style="color: #0000ff;"><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span><span style="color: #008080;">12</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="Submit"</span> <span style="color: #0000ff;">/></span><span style="color: #008080;">13</span> <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #008080;">16</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span>
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">if</span> ((<span style="color: #800080;">$_FILES</span>["file"]["size"] )<span style="color: #008080;"> 3</span> <span style="color: #000000;"> {</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_FILES</span>["file"]["error"] > 0<span style="color: #000000;">)</span><span style="color: #008080;"> 5</span> <span style="color: #000000;"> {</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">echo</span> "Return Code: " . <span style="color: #800080;">$_FILES</span>["file"]["error"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">else</span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> {</span><span style="color: #008080;">10</span> <span style="color: #0000ff;">echo</span> "Upload: " . <span style="color: #800080;">$_FILES</span>["file"]["name"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;">11</span> <span style="color: #0000ff;">echo</span> "Type: " . <span style="color: #800080;">$_FILES</span>["file"]["type"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;">12</span> <span style="color: #0000ff;">echo</span> "Size: " . (<span style="color: #800080;">$_FILES</span>["file"]["size"] / 1024) . " Kb<br>"<span style="color: #000000;">;</span><span style="color: #008080;">13</span> <span style="color: #0000ff;">echo</span> "Temp file: " . <span style="color: #800080;">$_FILES</span>["file"]["tmp_name"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">file_exists</span>("upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">]))</span><span style="color: #008080;">16</span> <span style="color: #000000;"> {</span><span style="color: #008080;">17</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_FILES</span>["file"]["name"] . " already exists. "<span style="color: #000000;">;</span><span style="color: #008080;">18</span> <span style="color: #000000;"> }</span><span style="color: #008080;">19</span> <span style="color: #0000ff;">else</span><span style="color: #008080;">20</span> <span style="color: #000000;"> {</span><span style="color: #008080;">21</span> <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],<span style="color: #008080;">22</span> "upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">]);</span><span style="color: #008080;">23</span> <span style="color: #0000ff;">echo</span> "Stored in: " . "upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">];</span><span style="color: #008080;">24</span> <span style="color: #000000;"> }</span><span style="color: #008080;">25</span> <span style="color: #000000;"> }</span><span style="color: #008080;">26</span> <span style="color: #000000;"> }</span><span style="color: #008080;">27</span> <span style="color: #0000ff;">else</span><span style="color: #008080;">28</span> <span style="color: #000000;"> {</span><span style="color: #008080;">29</span> <span style="color: #0000ff;">echo</span> "Invalid file"<span style="color: #000000;">;</span><span style="color: #008080;">30</span> <span style="color: #000000;"> }</span><span style="color: #008080;">31</span> ?>
$_FILES['userfile']['name']
$_FILES['userfile']['type']
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']
$_FILES['userfile']['error']
其中,$_FILES['userfile']['error']的所有值:
UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功。
UPLOAD_ERR_INI_SIZE 其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
UPLOAD_ERR_FORM_SIZE 其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。
UPLOAD_ERR_PARTIAL 其值为 3,文件只有部分被上传。
UPLOAD_ERR_NO_FILE 其值为 4,没有文件被上传。
UPLOAD_ERR_NO_TMP_DIR 其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。
UPLOAD_ERR_CANT_WRITE 其值为 7,文件写入失败。PHP 5.1.0 引进。
我们知道使用$_FILES['userfile']['type']判断上传文件类型是一个很不明智的做法,因为该判断依据是文件的后缀名,任何人都可以将一个mp3文件的后缀改成jpg从而伪装成图片进行上传,因此php官方建议使用php的扩展php_fileinfo来判断文件的mime,开启拓展的方法百度一下有很多,win和linux略有不同。
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">file_exists</span>("./upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">])) </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">do</span><span style="color: #000000;">{ </span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$suffix</span> =""<span style="color: #000000;">; </span><span style="color: #008080;"> 5</span> <span style="color: #800080;">$suffix_length</span> = 4<span style="color: #000000;">; </span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$str</span> = "0123456789abcdefghijklmnopqrstuvwxyz"<span style="color: #000000;">; </span><span style="color: #008080;"> 7</span> <span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$str</span>)-1<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">文件名后追加4个随机字符 </span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0 ; <span style="color: #800080;">$i</span>$suffix_length; <span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #008080;">10</span> <span style="color: #800080;">$suffix</span> .= <span style="color: #800080;">$str</span>[<span style="color: #008080;">rand</span>(0,<span style="color: #800080;">$len</span><span style="color: #000000;">)]; </span><span style="color: #008080;">11</span> <span style="color: #000000;"> } </span><span style="color: #008080;">12</span> <span style="color: #800080;">$upload_filename</span> = <span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]; </span><span style="color: #008080;">13</span> <span style="color: #800080;">$filename</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$upload_filename</span>,0,<span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$upload_filename</span>,".")).<span style="color: #800080;">$suffix</span>.".".<span style="color: #008080;">substr</span>(<span style="color: #800080;">$upload_filename</span>,<span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$_FILES</span>["file"]["name"],".")+1<span style="color: #000000;">); </span><span style="color: #008080;">14</span> }<span style="color: #0000ff;">while</span>(<span style="color: #008080;">file_exists</span>("./upload/".<span style="color: #800080;">$filename</span><span style="color: #000000;">)); </span><span style="color: #008080;">15</span> <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],"./upload/" . <span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #008080;">16</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #008080;">17</span> <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"], "upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">]); </span><span style="color: #008080;">18</span> }
<span style="color: #008080;">1</span> <span style="color: #800080;">$structure</span> = './'.<span style="color: #008080;">date</span>("Y").'/'.<span style="color: #008080;">date</span>("m").'/'.<span style="color: #008080;">date</span>("d").'/'<span style="color: #000000;">;</span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #008080;">4</span> <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$structure</span>, 0777, <span style="color: #0000ff;">true</span><span style="color: #000000;">)) {</span><span style="color: #008080;">5</span> <span style="color: #0000ff;">die</span>('Failed to create folders...'<span style="color: #000000;">);</span><span style="color: #008080;">6</span> <span style="color: #000000;">}</span><span style="color: #008080;">7</span> <span style="color: #008080;">8</span> <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],<span style="color: #800080;">$structure</span> . <span style="color: #800080;">$_FILES</span>["file"]["name"]);
<span style="color: #008080;">1</span> <span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #008080;">2</span> <span style="color: #0000ff;"><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">Pictures:</span><span style="color: #008080;">3</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="pictures[]"</span> <span style="color: #0000ff;">/></span><span style="color: #008080;">4</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="pictures[]"</span> <span style="color: #0000ff;">/></span><span style="color: #008080;">5</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="pictures[]"</span> <span style="color: #0000ff;">/></span><span style="color: #008080;">6</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="Send"</span> <span style="color: #0000ff;">/></span><span style="color: #008080;">7</span> <span style="color: #0000ff;"></span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #008080;">8</span> <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span></span></span></span></span></span></span>
<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span> <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$_FILES</span>["pictures"]["error"] <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$error</span><span style="color: #000000;">) {</span><span style="color: #008080;">3</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$error</span> ==<span style="color: #000000;"> UPLOAD_ERR_OK) {</span><span style="color: #008080;">4</span> <span style="color: #800080;">$tmp_name</span> = <span style="color: #800080;">$_FILES</span>["pictures"]["tmp_name"][<span style="color: #800080;">$key</span><span style="color: #000000;">];</span><span style="color: #008080;">5</span> <span style="color: #800080;">$name</span> = <span style="color: #800080;">$_FILES</span>["pictures"]["name"][<span style="color: #800080;">$key</span><span style="color: #000000;">];</span><span style="color: #008080;">6</span> <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$tmp_name</span>, "data/<span style="color: #800080;">$name</span>"<span style="color: #000000;">);</span><span style="color: #008080;">7</span> <span style="color: #000000;"> }</span><span style="color: #008080;">8</span> <span style="color: #000000;">}</span><span style="color: #008080;">9</span> ?>
有的情况下多文件的这种变量结构并不好用:
array(1) {
["upload"]=>array(2) {
["name"]=>array(2) {
[0]=>string(9)"file0.txt"
[1]=>string(9)"file1.txt"
}
["type"]=>array(2) {
[0]=>string(10)"text/plain"
[1]=>string(10)"text/html"
}
}
}
很多情况下我们需要的是类似这样的结构
array(1) {
["upload"]=>array(2) {
[0]=>array(2) {
["name"]=>string(9)"file0.txt"
["type"]=>string(10)"text/plain"
},
[1]=>array(2) {
["name"]=>string(9)"file1.txt"
["type"]=>string(10)"text/html"
}
}
}
使用下面的函数就能轻松转化结构
<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> diverse_array(<span style="color: #800080;">$vector</span><span style="color: #000000;">) { </span><span style="color: #008080;">2</span> <span style="color: #800080;">$result</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008080;">3</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$vector</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key1</span> => <span style="color: #800080;">$value1</span><span style="color: #000000;">) </span><span style="color: #008080;">4</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$value1</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key2</span> => <span style="color: #800080;">$value2</span><span style="color: #000000;">) </span><span style="color: #008080;">5</span> <span style="color: #800080;">$result</span>[<span style="color: #800080;">$key2</span>][<span style="color: #800080;">$key1</span>] = <span style="color: #800080;">$value2</span><span style="color: #000000;">; </span><span style="color: #008080;">6</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; </span><span style="color: #008080;">7</span> <span style="color: #000000;">} </span><span style="color: #008080;">8</span> <span style="color: #800080;">$upload</span> = diverse_array(<span style="color: #800080;">$_FILES</span>["upload"]);
首先,在表单上
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="MAX_FILE_SIZE"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="字节"</span> <span style="color: #0000ff;">/></span></span>
可以限制上传文件大小(可以被绕过)。
然后在服务器上也需要调整一下配置
php.ini:
max_execution_time = 30 每个脚本运行的最长时间,单位秒
max_input_time = 60,每个脚本可以消耗的时间,单位也是秒
memory_limit = 128M,这个是脚本运行最大消耗的内存
post_max_size = 8M,表单提交最大数据为 8M,此项不是限制上传单个文件的大小,而是针对整个表单的提交数据进行限制的。
upload_max_filesize = 2M ,上载文件的最大许可大小
nginx:
<span style="color: #008080;">1</span> <span style="color: #000000;">location / {</span><span style="color: #008080;">2</span> root html<span style="color: #008000;">;</span><span style="color: #008080;">3</span> index index.html index.htm<span style="color: #008000;">;</span><span style="color: #008080;">4 </span>client_max_body_size 1000m<span style="color: #008000;">;</span><span style="color: #008080;">5</span> }