多个文件没有上传,没有在php中移动文件

时间:2015-02-19 21:35:13

标签: php arrays file-upload

我试图上传多个文件,但我的代码绕过了“move_uploaded_file”代码。缺少什么?

foreach ($_FILES['file']['name'] as $file) {
    $target_dir = "uploads/";
    $target_file = $target_dir . $file;
        if (move_uploaded_file($file, $target_file)) {
            echo "The file ".$_FILES["file"]["name"]. " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
}

2 个答案:

答案 0 :(得分:0)

move_uploaded_file需要获取临时文件名:$_FILES['file']['tmp_name']

foreach ($_FILES as $file) {
    $target_dir = "./";
    $target_file = $target_dir . $file['name'];
    if (move_uploaded_file($file['tmp_name'], $target_file)) {
        echo "The file " . $file["name"] . " has been uploaded.<br />";
    } else {
        echo "Error uploading the file " . $file["name"] . ".<br />";
    }
}

上传表单的小型HTML代码段:

<form action="./testpage.php" method="post" enctype="multipart/form-data">
    <input name="file1" type="file" /><br />
    <input name="file2" type="file" /><br />
    <input type="submit" value="Upload!" />
</form>

答案 1 :(得分:0)

试试这个:

for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
    {
     if(!is_uploaded_($_FILES['file']['tmp_name'][$i]))
     {
    $messages[] = 'No  uploaded';
    }
    else
     {
     if(@copy($_FILES['file']['tmp_name'][$i],$target.'/'.$_FILES['file']['name'][$i]))
     {
      $messages[] = $_FILES['file']['name'][$i].' uploaded';
     }
     else
    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
     }
     }
     }

HTML:

<form enctype="multipart/form-data" action="#" method="post">
<input id="uploadFile" name="file[]" type="file" />
<input id="uploadFile" name="file[]" type="file" />
<input type="submit" value="Upload" name="uploadt"  />
</form>