While working on a Laravel project when you try to upload images or files that are large in size, you may encounter the 413 error in your Laravel application.
When you try to upload a file having a size larger than default allowed file size by the server. This makes the 413 error comes up.
There are two solutions to solve this, we are going to show how they will work with code examples.
- You should either validate the file size before uploading. This won’t allow a file/image of size greater than default allowed file size to get uploaded.
- You can also increase the default allowed file size of server if your application deals with more large sized files or images.
Solutions to 413 error
Solution 01:
The code below can be added in a controller file, when storing user or file data. Adding it to the validate function, the max:3000 will prevent files from uploading with a larger size.
$this->validate($request,[
'member_image' => 'mimes:jpeg,jpg,png|image|max:3000'
]);
Solution 02:
Increasing the client_max_body_size variable to allow files of large size will avoid the 413 error for files under the new specified value.
You should set the maximum allowed size of the client request body, you can specify it in the “Content-Length” request header field. If the request size exceeds the configured value, the 413 (Request Entity Too Large) error returns to the client. Please be aware that browsers may not correctly display this error. Setting size to 0 disables checking of client request body size.
You can usually set it 200 megabytes, as it will allow users to upload larger files using our dashboard.
// Need to increase or set client_max_body_size in your nginx config file.
Add ‘client_max_body_size 200M’ inside the http section in /etc/nginx/nginx.conf
http {
#...
client_max_body_size 200m;
#...
}
// Restart nginx
Moreover, you can also check in PHP settings.
grep -E "upload_max_filesize|memory_limit|post_max_size" /etc/php5/fpm/php.ini