本文主要给大家详细分析了php实现单文件上传和多文件上传的代码以及问题解决方案,一起学习参考下,希望能帮助到大家。
$_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.
相关推荐:
一个php文件上传类分享_php实例
php实现多文件上传的一种方法
实例讲解php实现常用文件上传类
以上就是实例分析php单文件和多文件上传的详细内容。