【问题标题】:Uploading images inside array not working上传数组内的图像不起作用
【发布时间】:2023-04-07 01:13:01
【问题描述】:

我想上传多张图片。 img[] 包含所有要上传的图像文件。这些值已成功插入到commas(,) 内爆的数据库中。但是图像没有上传到名为photos 的指定文件夹中。

<input type="file" name="img[]" id="img[]" />


$n=$_FILES["img"]["name"];
$t=$_FILES["img"]["tmp_name"];
$image=implode(",",$n);


             $ex=explode(",",$image);
         $i=0;
         foreach($ex as $item)
         {

             move_uploaded_file($_FILES["img[$i]"]["tmp_name"],"photos/$ex[$i]");

             $i++;
         }

【问题讨论】:

  • 对不起,你的代码很糟糕。倍增文件上传不是“5 分钟做事”。如果有人会用&lt;?php phpinfo() 上传 php 脚本,你可能不会想到会这样。您需要编写一个处理文件验证(包括大小和扩展名)的类
  • 另外,move_uploaded_file() 在成功时返回 TRUE,在失败时返回 FALSE。在您的代码中没有任何地方可以检查 - 所以您不知道它们是否已上传
  • 你试过var_dump($_FILES)看看这个数组有什么结构吗?因为当您上传许多文件时,它应该是多维数组。阅读此php.net/manual/en/features.file-upload.multiple.php

标签: php image file-upload


【解决方案1】:

我认为你的正确代码应该是

<input type="file" name="img[]" id="img[]" />


$n=$_FILES["img"]["name"];
$t=$_FILES["img"]["tmp_name"];
$image=implode(",",$n);

 // no need to explode here

     foreach($n as $key=>$item)
     {
         //name will be there in $items 
        // use temp_name of same file for which you are using name using $_FILES["img"]["tmp_name"][$key]
         move_uploaded_file($_FILES["img"]["tmp_name"][$key],"photos/$item");
     }

【讨论】:

  • 太棒了.. 很好用.. 非常感谢.. @dreamCoder
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 2015-02-08
  • 2020-07-12
相关资源
最近更新 更多