
表单 HTML :
<form method="post" enctype="multipart/form-data"> <input type="file" name="file[]" multiple="multiple"> </form> php 端通过 $_FILES 获到的值:
Array ( [file] => Array ( [name] => Array ( [0] => 1.jpg [1] => 2.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg ) [tmp_name] => Array ( [0] => /tmp/php330E.tmp [1] => /tmp/php330F.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 59420 [1] => 300300 ) ) ) 请问 html 元素里有什么属性可以让 php 端通过 $_FILES 获取到的值变成这样,或者 php 里有什么非常简便的方法、函数可以变成这样:
Array ( [file] => Array ( [0] => Array ( [name] => 1.jpg [type] => image/jpeg [tmp_name] => /tmp/php330E.tmp [error] => 0 [size] => 59420 ) [1] => Array ( [name] => 2.jpg [type] => image/jpeg [tmp_name] => /tmp/php330F.tmp [error] => 0 [size] => 300300 ) ) ) 1 hanzichi 2016-11-19 18:30:32 +08:00 一般都是用 name 属性吧 ... |
2 liuxu 2016-11-19 23:03:57 +08:00 |
4 lslqtz 2016-11-20 02:45:09 +08:00 楼主,提个问题,这样你会丢失掉传过来的 name 名。 |
5 lslqtz 2016-11-20 02:50:23 +08:00 我就只能做到这样了,效率什么的。。懒得管了 <form method="post" enctype="multipart/form-data"> <input type="file" name="f1"> <input type="file" name="f2"> <input type="submit"> </form> <?php if (count($_FILES) != 1) { $filekeys=array_keys($_FILES); for ($i=0;$i<count($filekeys);$i++) { $nowfile=$_FILES[$filekeys[$i]]; $files['file'][$i]=['name'=>$nowfile['name'],'type'=>$nowfile['type'],'tmp_name'=>$nowfile['tmp_name'],'error'=>$nowfile['error'],'size'=>$nowfile['sze']]; } unset($filekeys,$nowfile); } print_r($files); ?> |
6 lslqtz 2016-11-20 02:51:53 +08:00 你可以把它用 function 写成一个函数,然后传出数组来 |
7 lslqtz 2016-11-20 02:52:44 +08:00 不过似乎。。忘了考虑单文件下的了, if 后面加个 else 来直接 print 或者拼起来吧 |
8 chaegumi 2016-11-20 08:25:37 +08:00 不用自己写,用现成的组件 http://fex.baidu.com/webuploader/ |
9 duo6duo6 2016-11-20 16:29:47 +08:00 |