2013-04-01 117 views
0

我想上传多张图片。 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++; 
     } 
+0

对不起的人,但你的代码是坏的。多文件上传不是“5分钟做事”。如果有人会用'<?php phpinfo()'上传php脚本,那么你可能不会这么想。您需要编写一个可处理文件验证的类(包括大小和扩展名) – Yang

+0

另外,'move_uploaded_file()'成功返回'TRUE',失败时返回'FALSE'。在你的代码中无处检查 - 你不知道它们是否已经上传 – Yang

+0

你试过'var_dump($ _ FILES)'来看看这个数组有什么结构?因为当你上传很多文件时它应该是多维数组。阅读此http://www.php.net/manual/en/features.file-upload.multiple.php – piotrekkr

回答

0

我觉得你的正确的代码应该是

<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"); 
    } 
+0

伟大..它wOrks ..非常谢谢.. @dreamCoder – Anoop

+0

@Anoop感谢没有理由':) “很高兴帮助你 – alwaysLearn