Laravel File Upload fails with "The file failed to upload."

September 11th, 2024

debugging-laravel-file-failed-to-upload.png

Today, I debugged an issue with a Laravel site that happened when a client tried to upload an 8mb PDF file. The controller contained validation to check the allowed file type but I found that the validation failed before hitting my custom rules. The error data returned from the server in the 422 response was:

{
    "message": "The file failed to upload.",
    "errors": {
        "file": [
            "The file failed to upload."
        ]
    }
}

The reason this happened was because of the file upload limits set in the php.ini file. Adjusting them to the following resolved the issue:

post_max_size=16M
upload_max_filesize=16M

Remember to restart PHP-FPM after making the change.