我正在尝试允许在表单中上传多个图像。但是它并不完全有效,我无法发现问题。
表格如下所示:
<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)
}
}
}