迭代多个上传$ _FILES数组

时间:2012-11-29 03:20:46

标签: php file-upload foreach

我的网站上有两个表单,一个上传单个文件,另一个上传多个文件。两个表单都上传到同一目录,单个文件表单正在完美运行。为了优化多文件上传表单的代码,我没有单独写出每个文件的处理,我有以下代码:

foreach($_FILES['multi']['name'] as $uploaded_file){
     if(!move_uploaded_file($uploaded_file, "/var/www/uploads/".$uploaded_file."")){
         echo "Could not move File: ".$uploaded_file."<br />";
     }else{
         // Do additional processing
     }  
}

而不是将文件移动到目录,这是有效的,适用于单个文件上载表单,脚本错误输出并显示我的停止消息:

"Could not move File: <filename here>"

一直在抓我的头脑,感谢任何见解和帮助!

2 个答案:

答案 0 :(得分:1)

您没有正确定位上传的临时文件位置:

foreach ($_FILES['multi']['name'] as $idx => $uploaded_file) {
    move_uploaded_file($_FILES['multi']['tmp_name'][$idx], "/var/www/uploads/$uploaded_file");
}

答案 1 :(得分:0)

尝试类似:

$count=0; 
foreach($_FILES['multi']['name'] as $uploaded_file) {
   $tmp=$_FILES['file']['tmp_name'][$count];
   if(!move_uploaded_file($tmp, "/var/www/uploads/".$uploaded_file)){
     echo "Could not move File: ".$uploaded_file."<br />";
   }else{
     // Do additional processing
   }   
   $count=$count + 1;
}