我正在尝试使用 ajax 和 php 上传多个文件。 JavaScript和Ajax代码如下;
$(document).on('click', '#UploadButton', function(e) {
var form = new FormData();
var files = document.getElementsByClassName('receipts');
for (var i=0; i<files.length; i++) {
form.append("files[receipt" + i + "]", files[i][0]); // add receipt to form
}
form.append('action', 'upload-receipts'); // specify action
$.ajax({
url: 'handler.php',
type: 'POST',
data: form,
cache: false,
processData: false,
contentType: false,
success:function(data) {
console.log(data);
},
error: function(xhr, desc, err) {
// I have some error handling logic here
}
});
});
PHP 处理程序例程如下;
$action = $_REQUEST['action'];
switch($action) {
case 'upload-receipts':
$files = $_FILES['files'];
$no_files = count($_FILES["files"]['name']);
exit(json_encode(['size'=>$no_files]));
/* for ($i = 0; $i < $no_files; $i++) {
if ($_FILES["files"]["error"][$i] != 0) {
move_uploaded_file($_FILES["files"]["tmp_name"][$i], 'uploads/' . $_FILES["files"]["name"][$i]);
}
} */
break;
case 'download-file':
break;
default:
exit(json_encode(['success'=>false, 'message'=>'InvalidActionException']));
}
我最多能够成功上传 20 个文件.
我的问题是我无法上传超过 20 个文件。每次我尝试上传超过 20 个文件时,只有前 20 个上传失败,其余的都失败。如图所示退出脚本仅表明
size = 20。有人可以帮助我理解并确定为什么会出现这种情况以及该问题的解决方案。