您的位置:首页 > 编程语言 > PHP开发

PHP单文件和多文件上传实例

2017-12-12 16:09 495 查看

$_FILES何时为空数组?

表单提交 enctype 不等于 multipart/form-data 的时候 php.ini配置文件中,file_uploads = Off 上传的文件大小 > php.ini配置文件中所配置的最大上传大小时

只要出现 $_FILES 为空数组,就可能出现以上的问题,必须修复!

如果 未选择任何文件 就马上点击 “上传按钮”,$_FILES将会是一个有元素的数组,元素中的每个属性都是空字符串,error属性为4

单文件上传

$_FILES 数据结构

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

多文件上传

相比单文件上传,多文件上传处理起来要复杂多了前端的两种多文件上传形式

//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
对应的数据结构不同

//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.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息