【发布时间】:2019-04-24 14:46:50
【问题描述】:
我写了这个简短的脚本来上传多个文件,基于 Stackoverflow 的一个旧主题:How do you loop through $_FILES array? (我没有回复它,因为它已经 7 岁了......如果我做错了请告诉我)
if(!empty($_FILES)) {
$i = 0;
foreach($_FILES['images']['tmp_name'] as $index => $tmpName) {
if(!empty($tmpName) && is_uploaded_file($tmpName)) {
$img_url = url_rewrite($intitule).'-' . time() . $i . '.'.strtolower(substr(strrchr($tmpName, '.'),1));
$gallery = $gallery . '|' . $img_url;
move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
}
$i++;
}
}
echo $gallery;
所以基本上,我在 $_FILES['images'] 中发送多个文件,并在上传之前创建唯一名称(使用 time() + $i)。我从来没有得到文件扩展名,因为 $_FILES['images']['name'] 似乎是空的。这不是因为 Var_dump 返回一个包含我需要的所有东西的完整数组:
array(1) {
["images"]=> array(5) {
["name"]=> array(5) {
[0]=> string(13) "my-file-1.jpg"
[1]=> string(13) "my-file-2.jpg"
[2]=> string(13) "my-file-3.jpg"
[3]=> string(13) "my-file-4.jpg"
[4]=> string(13) "my-file-5.jpg"
}
["type"]=> array(5) {
[0]=> string(10) "image/jpeg"
[1]=> string(10) "image/jpeg"
[2]=> string(10) "image/jpeg"
[3]=> string(10) "image/jpeg"
[4]=> string(10) "image/jpeg"
}
["tmp_name"]=> array(5) {
[0]=> string(14) "/tmp/php1Nrgb4"
[1]=> string(14) "/tmp/phpnIJHZa"
[2]=> string(14) "/tmp/phpcAEf1c"
[3]=> string(14) "/tmp/phpbHgrVj"
[4]=> string(14) "/tmp/phpGu0FIp"
}
["error"]=> array(5) {
[0]=> int(0)
[1]=> int(0)
[2]=> int(0)
[3]=> int(0)
[4]=> int(0)
}
["size"]=> array(5) {
[0]=> int(262684)
[1]=> int(15644)
[2]=> int(32638)
[3]=> int(11897)
[4]=> int(103303)
}
}
}
我还需要 ['type'] 来测试文件,但同样的事情:我无法返回数组的内容。
你发现这个脚本有什么问题吗?
【问题讨论】:
-
"
$_FILES['images']['name']好像是空的。" ????从您的var_dump()可以清楚地看出它远非空 -
这就是我寻求帮助的原因。我在 $_FILES['images']['name'] 而不是 'tmp_name' 上尝试了 Foreach,但什么也没出现。