I am trying to upload multiple files using ajax and php. The JavaScript and Ajax code is as follows;
$(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
}
});
});
The PHP handler routine is as follows;
$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']));
}
I am able to successfully upload utmost 20 files.
My problem is that I am unable to upload more than 20 files. Each time I attempt to upload more than 20 files, only the first 20 upload the rest fail. Exiting the script as shown only indicates the
size = 20. May someone help me understand & identify why this is the case and a solution to this problem.