Laravel file uploads limits

Nav • August 23, 2020


Over the years, Laravel file uploads have gotten so simple that most of the time they just work. Livewire has also added file uploads to its api. However there are some issues that can pop up when dealing server limits, write permissions and some other edge cases.

php.ini limits

Most common issue is the setting in php.ini file. These are very simple to update. Start by checking the current limits.

Lets check the maximum upload file size:

php -i | egrep -i '^(upload_max_filesize|post_max_size)'

This will produce the following output:

post_max_size => 5M => 5M
upload_max_filesize => 5M => 5M

We want to update two settings, max_file_uploads and post_max_size. A good starting point is up the limit to 200MB.

Start by checking the location of your php.ini.

php -i | grep 'Loaded Configuration File'
/* Output:
Loaded Configuration File => /etc/php/7.4/cli/php.ini

Open /etc/php/7.4/cli/php.ini in your choice of editor.

pico /etc/php/7.4/cli/

Update the two settings to:

upload_max_filesize = 200M
post_max_size = 200M

Restart Apache for the settings to take effect.

sudo systemctl restart apache2

.htaccess settings

There have been times that updating the php.ini settings isn't enough. Next to update is the .htaccess file.

In a laravel project, this is located in public folder.

Inside the <IfModule mod_rewrite.c> tag add the following:

    # Handle file upload size
    php_value post_max_size 200M
    php_value upload_max_filesize 200M
    php_value max_execution_time 6000000

Also, be sure the file contains RewriteEngine On. In a laravel project that is usually set.

Between php.ini and .htaccess updates, you should now be able to upload larger files.