问题描述
我正在尝试使用ajax和php上传多个文件. JavaScript和Ajax代码如下;
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
}
});
});
PHP处理程序例程如下;
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']));
}
我能够成功上传最多20个文件.
我的问题是我无法上传超过20个文件.每次我尝试上载20个以上的文件时,只有前20个上载失败.如图所示退出脚本仅表示
size =20.可以有人帮助我了解&;确定为什么会这样,并解决此问题.
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.
推荐答案
这篇文章解决了我所面临的类似问题,我认为添加答案会很有用.
As this post resolved a similar issue I was facing I thought it would be useful to add an answer.
基于用户WillyMilimo
的上述评论:
修改通常在/etc/php/<php_version>/fpm/php.ini
目录中找到的php.ini
.
Modify the php.ini
typically found in the /etc/php/<php_version>/fpm/php.ini
directory.
将max_file_uploads
设置从默认值20
更改为所需的最大值.
Change the max_file_uploads
setting from its default of 20
to desired maximum.
重新启动php服务(例如,使用service php<php_version>-fpm restart
命令).
Restart the php service (e.g. using the service php<php_version>-fpm restart
command).
这篇关于使用AJAX和PHP上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!