我有这张表格可以将多张图片上传到我的画廊:
HTML代码
<form method="POST" enctype="multipart/form-data" action="gallery.php?action=upload&folder=1">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
PHP代码
$upl_count = count($_FILES['images']);
echo "Uploaded $upl_count images.";
无论我上传什么,$ upl_count总是5,如果我按以下方式迭代文件:
PHP代码
$images = $_FILES['images'];
foreach ($images as $file => $name)
{
echo $images['type'][$file];
}
...类型始终为空。后一部分没有任何回应。我在这里错过了什么?这可能是编码问题吗?
答案 0 :(得分:0)
我找到了解决问题的解决方案here。
所以,$_FILES
总是包含
Array (
[name] => Array
(
[0] => foo.txt
[1] => bar.txt
)
[type] => Array
(
[0] => text/plain
[1] => text/plain
)
[tmp_name] => Array
(
[0] => /tmp/phpYzdqkD
[1] => /tmp/phpeEwEWG
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 123
[1] => 456
)
)
不方便!同意?我们需要更明显的东西:
Array(
[0] => Array
(
[name] => foo.txt
[type] => text/plain
[tmp_name] => /tmp/phpYzdqkD
[error] => 0
[size] => 123
)
[1] => Array
(
[name] => bar.txt
[type] => text/plain
[tmp_name] => /tmp/phpeEwEWG
[error] => 0
[size] => 456
)
)
为了获得这样的数组,让我们使用这个装饰器函数:
function reorder_upload_files_array($file_post) {
$file_array = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i = 0; $i < $file_count; $i++) {
foreach ($file_keys as $key) {
$file_array[$i][$key] = $file_post[$key][$i];
}
}
return $file_array;
}
用法:
$files = reorder_upload_files_array($_FILES)