【问题标题】:Filled up $_FILES doesn't return file extension填充 $_FILES 不返回文件扩展名
【发布时间】: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,但什么也没出现。

标签: php arrays file loops


【解决方案1】:

$_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;

【讨论】:

  • 谢谢@RiggsFolly,它就是这样工作的。我想我忘记了 [$index]。但是为什么我不能使用 foreach($_FILES['images']['name'] as $index => $tmpName) 代替? (我保留了你的建议,但我很好奇)
  • 因为你需要,作为 TMP 文件的位置和名称,以便move_upload_file() 将移动 tmp 文件。
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
  • 2014-03-02
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2017-06-17
相关资源
最近更新 更多