php - 填满的 $_FILES 不返回文件扩展名

标签 php arrays file loops

我写了这个简短的脚本来上传多个文件,基于 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']['tmp_name'] 不包含扩展名,即由上传文件制成的 PHP 临时文件。

如果您想要从用户 PC 上传的文件的扩展名,您需要查看 $_FILES['images']['name']

所以

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($_FILES['images']['name'][$index], '.'),1));
                    // changed here -----------------------------^^^^^^^^^^^^^^^^
        $gallery = $gallery . '|' . $img_url;
        move_uploaded_file( $tmpName, $dir_upload . '/' . $img_url);
    }
    $i++;
}

你也可以简化那一堆得到扩展的函数

        $img_url = url_rewrite($intitule)
                    .'-' 
                    . time()
                    . $i 
                    . '.'
                    . pathinfo($_FILES['images']['name'][$index], PATHINFO_EXTENSION);

        $gallery = $gallery . '|' . $img_url;

关于php - 填满的 $_FILES 不返回文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53422118/

相关文章:

java - 数组/堆栈/队列的大 O 表示法

php - 单项执行出错

php - 存储过程将来自不同调用的结果混合到临时表中

php - 如何在PHP中分别求和二维数组的值

c - 从数组中删除重复项

java - 如何异步删除文件

javascript - 检查表中的所有数据值

php - JEA 设施布局添加自定义 css 样式

python - 打开文件对象的大小

写入后计算文件偏移量