$_FILES何時為空數組?
表單提交 enctype 不等于 multipart/form-data 的時候 php.ini配置文件中,file_uploads = Off 上傳的文件大小 > php.ini配置文件中所配置的最大上傳大小時
只要出現 $_FILES 為空數組,就可能出現以上的問題,必須修復!
如果 未選擇任何文件 就馬上點擊 “上傳按鈕”,$_FILES將會是一個有元素的數組,元素中的每個屬性都是空字符串,error屬性為4
單文件上傳
$_FILES 數據結構
1
2
3
4
5
6
7
8
9
|
array( 'filename' => array( 'name' => 'xxx.png', 'type' => 'image/png', 'size' => 2548863, 'tmp_name' => '/img/sdsdsd.png', 'error' => 0 ) ) |
無論是單文件
還是多文件上傳
,都會有5個固定屬性:name / size / type / tmp_name / error
多文件上傳
相比單文件上傳,多文件上傳處理起來要復雜多了前端的兩種多文件上傳形式
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//name相同 < form method = "post" enctype = "multipart/form-data" > < input type = "file" name = "wt[]" /> < input type = "file" name = "wt[]" /> < input type = "submit" value = "提交" /> </ form > //name不同(簡單點) < form method = "post" enctype = "multipart/form-data" > < input type = "file" name = "wt" /> < input type = "file" name = "mmt" /> < input type = "submit" value = "提交" /> </ form > |
后端的 $_FILES
對應的數據結構不同
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//name相同 array (size=1) 'wt' => array (size=5) 'name' => array (size=2) 0 => string '新建文本文檔 (2).txt' (length=26) 1 => string '新建文本文檔.txt' (length=22) 'type' => array (size=2) 0 => string 'text/plain' (length=10) 1 => string 'text/plain' (length=10) 'tmp_name' => array (size=2) 0 => string 'C:\Windows\php1D64.tmp' (length=22) 1 => string 'C:\Windows\php1D65.tmp' (length=22) 'error' => array (size=2) 0 => int 0 1 => int 0 'size' => array (size=2) 0 => int 0 1 => int 1820 //name不同(簡單點) array (size=2) 'wt' => array (size=5) 'name' => string '新建文本文檔 (2).txt' (length=26) 'type' => string 'text/plain' (length=10) 'tmp_name' => string 'C:\Windows\php39C7.tmp' (length=22) 'error' => int 0 'size' => int 0 'mmt' => array (size=5) 'name' => string '新建文本文檔.txt' (length=22) 'type' => string 'text/plain' (length=10) 'tmp_name' => string 'C:\Windows\php39D8.tmp' (length=22) 'error' => int 0 'size' => int 1820 |
字段Error用途
值:1 上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值。
值:2 上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。
值:3 文件只有部分被上傳。
值:4 沒有文件被上傳。值:5 上傳文件大小為0.
原文鏈接:https://segmentfault.com/a/1190000012301077