我正在使用这个地方的代码: Uploading Multiple Files using AJAX
最多上传 7 张图片(大约 1MB),当我在做第 8 张图片时(或者当我选择了等于或超过 8 张图片时),这个错误又回来了:
注意:未定义索引:第 10 行 E:\Apache Software Foundation\Apache2.2\htdocs\ReGeneSys\upload2.php 中的图像
警告:在第 10 行的 E:\Apache Software Foundation\Apache2.2\htdocs\ReGeneSys\upload2.php 中为 foreach() 提供的参数无效
HTML 是:
<div id="main" style="width:40%;margin:0 auto;text-align:center;" ><br/>
<h4>Use this if you want to upload multiple images</h1>
<form method="post" enctype="multipart/form-data" action="upload2.php">
<input type="file" name="images" id="images" multiple />
<input type="hidden" name="logId" id="logId" value="<?php echo $_REQUEST['log_id'] ?>" />
<button type="submit" id="btn">Upload Files!</button>
</form>
<div id="response"></div>
<ul id="image-list">
</ul>
</div>
JS是:
var logId= document.getElementById("logId").value;
//alert(logId);
(function () {
var input = document.getElementById("images"),
formdata = false;
/* function showUploadedItem (source) {
var list = document.getElementById("image-list"),
li = document.createElement("li"),
img = document.createElement("img");
img.src = source;
li.appendChild(img);
list.appendChild(li);
} */
if (window.FormData) {
formdata = new FormData();
document.getElementById("btn").style.display = "none";
}
input.addEventListener("change", function (evt) {
document.getElementById("response").innerHTML = "Uploading . . ."
var i = 0, len = this.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
/*reader.onloadend = function (e) {
showUploadedItem(e.target.result, file.fileName);
};*/
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "upload2.php?logId="+logId,
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
}, false);
}());
PHP 是:
$log_id=$_REQUEST['logId'];
print_r($_FILES);
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
$path="screens/";
$append_Name=getScalar("SELECT (COALESCE(MAX(screen_id),1)+1) FROM screenshots LIMIT 1");
//print_r($_FILES);
//$image_name=$_FILES['images']['tmp_name'][$key];
$name=$append_Name."_".$name;
move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "screens/" . $name);
..// other part of the PHP
}
}
注意 getScalar 是一个返回结果源的第一个值的函数。
您能否帮我了解第 7 张图片后出现的问题。再次提到,代码工作得非常好,直到我还没有上传大约 1MB 大小的第 8 张图像。
错误追溯到这一行:
foreach ($_FILES["images"]["error"] as $key => $error)
谢谢。