0

我正在尝试使用一个表单帖子上传两个文件,但是我的表单只会上传第一个文件,并且不会遍历第二个文件并上传。知道为什么吗?我正在使用的代码如下。

<?php

$upload = $_POST['upload'];
$enteredPassword = $_POST['password'];
$uploadedFiles = $_FILES;
$password = "************";

// Where the file is going to be placed 
$target_path = "";

//interger count
$i = 0;

//upload the files
if ($upload == true && $enteredPassword == $password) {
foreach($uploadedFiles as $uploadedFile) {
    $target_path = $target_path . $uploadedFile['name'][$i]; 

    if(move_uploaded_file($uploadedFile['tmp_name'][$i], $target_path)) {
        echo "<p>The file ".  $uploadedFile['name'][$i]. 
        " has been uploaded.</p>";
    } else{
        echo "<p>There was an error uploading the {$uploadedfile['name']}, please try again!</p>";
    }
    $i++;
}
}
?>

<ul>
<form enctype="multipart/form-data" action="" method="post">
    <input type="hidden" name="upload" value="true" />
    <li>Choose a file to upload:</li>
    <li><input name="userfile[]" type="file" size="40" /></li>
    <li><input name="userfile[]" type="file" size="40" /></li>
    <li>Enter password for file upload:</li>
    <li><input name="password" type="password" size="40" /></li>
    <li><input type="submit" value="Upload File" /></li>
</form>
</ul> 
4

3 回答 3

0

如果您愿意,可以使用此插件进行多张图片上传。它对我很有用... http://www.plupload.com/example_queuewidget.php

于 2012-06-01T03:44:48.577 回答
0

试试,uploadify 有很好的 api 和回调的多次上传。 http://www.uploadify.com/

于 2012-06-01T03:55:42.413 回答
0

这种行为是意料之中的,因为您的 $_FILES 数组中只有一个元素是 $_FILES["userfile"]。你可以尝试这样的事情来实现你想要的:

<?php
if(isset($_POST["submit"]))
{
    $upload = $_POST['upload'];
    $enteredPassword = $_POST['password'];
    $uploadedFiles = $_FILES["userfile"];
    $password = "12345";

    // Where the file is going to be placed 
    $base_path = "upload/";

    //upload the files
    if ($upload == true && $enteredPassword == $password) {
        foreach(array_combine($uploadedFiles["name"], $uploadedFiles["tmp_name"]) as $name => $tmp_name) {
            $target_path = $base_path .$name; 

            if(move_uploaded_file($tmp_name, $target_path)) {
                echo "<p>The file ".  $name." has been uploaded.</p>";
            }
            else
            {
                echo "<p>There was an error uploading the {$name}, please try again!</p>";
            }
        }
    }
}
?>

<ul>
<form enctype="multipart/form-data" action="" method="post">
    <input type="hidden" name="upload" value="true" />
    <li>Choose a file to upload:</li>
    <li><input name="userfile[]" type="file" /></li>
    <li><input name="userfile[]" type="file" /></li>
    <li>Enter password for file upload:</li>
    <li><input name="password" type="password" size="40" /></li>
    <li><input type="submit" name="submit" value="Upload File" /></li>
</form>
</ul> 
于 2012-06-01T14:17:50.747 回答