多图像上传失败

时间:2013-01-27 05:01:56

标签: php html5 file image-upload

我正在尝试允许在表单中上传多个图片。然而,它并没有完全奏效,我无法发现问题。

表格如下:

<form method="post" action="add-property.php" enctype="multipart/form-data">
<input type="file" multiple="multiple" name="files[]" />
<input type="submit" name="submit" class="property-submit" value="Submit Property" />
</form>

后面的PHP(在同一页面上)是:

//Image logic    
for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
    if (($_FILES['files']['type'] == "image/jpeg") || ($_FILES['files']['type'] == "image/png") || ($_FILES['files']['type'] == "image/jpg")) {
        if ($_FILES['files']["error"] > 0) {
            echo "Return Code: ".$_FILES['files']["error"]."<br />";
        }
        else {
            $target = $_SERVER['DOCUMENT_ROOT'].'/images/property-images/';
            if (file_exists($target.$currUser.$_FILES['files']['name'])) {
                echo $file["name"] . "already exists.";
            }
            else {
                move_uploaded_file($_FILES['files']["tmp_name"],
                $target.$currUser.$_FILES['files']["name"]);
                echo "Stored in: ".$target.$currUser.$_FILES['files']["name"];
            }
        }
    }
    else {
        echo 'error';
    }
}

$ currUser是用户的当前用户名。

问题是 - 当我尝试上传文件时。什么都没发生。它回复了一个错误但文件类型肯定是正确的。没有图像发送到该文件夹​​。我哪里可能出错?

编辑 - 请求的var转储:

array(1) {
  ["files"]=>
  array(5) {
    ["name"]=>
    array(1) {
      [0]=>
      string(17) "Chrysanthemum.jpg"
    }
    ["type"]=>
    array(1) {
      [0]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(1) {
      [0]=>
      string(14) "/tmp/php1WEAjj"
    }
    ["error"]=>
    array(1) {
      [0]=>
      int(0)
    }
    ["size"]=>
    array(1) {
      [0]=>
      int(879394)
    }
  }
}

2 个答案:

答案 0 :(得分:1)

排除count($_FILES['files']['name']),您需要为每个[$i]个引用添加$_FILES,例如$_FILES['files']["tmp_name"][$i]

此外,$file["name"]应为$_FILES['files']["name"][$i]

答案 1 :(得分:-1)

for ($i = 0; $i < count($_FILES['file']['name']); $i++) {

if (($_FILES['file']['type'][$i] == "image/jpeg") || ($_FILES['file']['type'][$i] == "image/png") || ($_FILES['file']['type'][$i] == "image/jpg")) {
    if ($_FILES['file']["error"][$i] > 0) {
        echo "Return Code: ".$_FILES['file']["error"][$i]."<br />";
    }
    else {

        if (file_exists("upload/".$_FILES['file']['name'][$i])) {
            echo $_FILES['file']["name"][$i] . "already exists.<br>";
        }
        else {
            move_uploaded_file($_FILES['file']["tmp_name"][$i],
                "upload/".$_FILES['file']["name"][$i]);
            echo "Stored in: ". "upload/" .$_FILES['file']["name"][$i];
        }
    }
}
else {
    echo 'error';
}

}