How to handle if uploaded file exceeds the upload_max_filesize in Laravel in best way?

Laravel applications use many directives for different purposes. In order to preserve your server’s resources, hosts set a limit to allow a maximum size for a file to be uploaded. This maximum size is in megabytes, and is defined in the upload_max_filesize directive. You might face an error in uploads if the uploaded file exceeds the upload_max_filesize.

The upload_max_filesize directive is is located in the php.ini file. It is a default server configuration file for the applications that require PHP.

It is one of the cause of fails in video uploads, when an uploaded file exceeds the upload_max_filesize. When dealing with large files like heavy images or video’s, we will need to update the default maximum size defined in the upload_max_filesize directive. You can do this in several ways.

Edit php.ini using cPanel

If your Laravel application is hosted in cPanel, you can to edit your php.ini file and upload_max_filesize directive using the cPanel dashboard. You can choose the directive from a dropdown and increase the value as per your needs.

Edit php.ini using FTP

You can connect to your server hosting the Laravel application via FTP. Navigate to your application’s root folder. If you see the php.ini file in the root folder, you can edit the file. Otherwise, create a new file and name it php.ini. You can add in it the code below.

upload_max_filesize = 12M
post_max_size = 13M
memory_limit = 15M

You may limit the upload_max_filesize based on your requirement.

Since, php.ini file controls your server operations for PHP applications.

You may or may not be able to use php.ini files, as per the host restrictions. So, a more reliable approach can be to use .htaccess.

Increase upload_max_filesize using .htaccess

If you cannot access the php.ini file, you can also try to modify the upload_max_filesize directive by editing your application’s .htaccess file.

You can connect to your server hosting the Laravel application via FTP. Navigate to your application’s root folder. Here you can find the .htaccess file.

Then, you may add the same code snippet as above, to adjust the values based on your needs.

If you get any internal server error message after adding these codes, your server is likely running PHP in CGI mode, that means you cannot add these commands in your .htaccess file. You should then remove the snippets you just added and your application should start functioning again.

Leave a Comment