2

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.

WillyMilimo
  • 447
  • 3
  • 12

1 Answers1

0

As this post resolved a similar issue I was facing I thought it would be useful to add an answer.

Based on the above comment of user WillyMilimo:

Modify the php.ini typically found in the /etc/php/<php_version>/fpm/php.ini directory.

Change the max_file_uploads setting from its default of 20 to desired maximum.

Restart the php service (e.g. using the service php<php_version>-fpm restart command).

macourtney7
  • 521
  • 2
  • 10
  • 24