Validate Max File Size in Laravel, PHP and Web-Server

I got this question at least a dozen times: “I’m raising my file validation rule in Laravel to 20 MB but still get errors, it doesn’t work”. So decided to expand on it and explain the reason, and what to do.

First, let’s see how to validate file size in Laravel. In your Form Request files or validate() method you can pass array with this parameter:

  'image' => 'required|mimes:jpeg,bmp,png|size:20000',

The last part means that size should be not more than 20 MB (20000 kB). But that may be not enough, cause file restrictions exist not only on Laravel application level.

PHP settings in php.ini

There are two settings related to max size in php.ini file. Here they are with their default values:

upload_max_filesize = 2M
post_max_size = 8M

As you can see, by default you can upload files only up to 2 MB, so you should change that to 20M. But even that won’t allow you to upload 20 MB files, because of overall POST request restriction by post_max_size, it should be set to 20M, or rather even bigger than that, cause POST will likely to have more data than just the file, so I would set it to 21M at least, in that case.

Also, please make sure you’re editing the correct php.ini file, cause there are cases with multiple files on the same server, for different PHP versions, also for FPM and CLI settings.

But even this may be not enough. There’s also web-server configuration.

Nginx and Apache Settings

By default Laravel Forge creates servers with LEMP stack, which uses Nginx as a web-server. Even without knowing how to configure it, you need to care about one setting in nginx.conf file: client_max_body_size.

Here’s a screenshot from official documentation:

As you can see, default value is only 1m, which means that your whole POST request may be maximum 1MB. So you need to change that setting to 20m or higher.

If you’re using Apache web-server, there’s also a setting for that called LimitRequestBody. Here’s a screenshot from the docs:

The difference here is that Apache doesn’t give any restrictions by default. So there’s a chance that you will never need to edit this value, but just in case, it’s good to know it exists.

So these are my tips on how to upload bigger files in Laravel. Anything I’ve missed?

Like our articles?
Check out our Laravel online courses!


  1. Size is the wrong validation rule to use for this use case.

    From the Laravel doku for size:

    // Validate that an uploaded file is exactly 512 kilobytes…
    ‘image’ => ‘file|size:512’;

    From the Laravel doku for max:

    The field under validation must be less than or equal to a maximum value

  2. QUERY: I have 3 radio buttons. 1st is portrait, 2nd is landscape and 3rd is square. I need validation on all radios with different rules, different messages on specific selection, and dimension validations.
    Maybe my code is helpful, I have 3 radio buttons and I need to apply validation on them. If user choose 1st radio selection then the validation rules will be different like different image dimension(min_width, min_height, max_width, max_height), and if user choose 2nd radio selection then the validation rule will be different and so on. I read many articles and found solution, we can do this type of validation using custom rules and messages in laravel. I mean using Laravel Custom Validation.

    public function artwork(Request $request)
    $rules = [
    ‘art_id’ => ‘string|max:200’,
    ‘artist_name’ => ‘required|string|max:200’,
    ‘title’ => ‘required|string|max:200’,
    ‘detail’ => ‘required|string|max:5000’,
    ‘weight’ => ‘string|max:200’,
    ‘length’ => ‘string|max:200’,
    ‘width’ => ‘string|max:200’,
    ‘height’ => ‘required|numeric’,
    ‘medium’ => ‘required|string|max:200’,
    ‘date_created’ => ‘date’,
    ‘art_style’ => ‘required|numeric’,
    ‘availability’ => ‘required|string|max:200’,
    ‘edition’ => ‘required|string|max:200’,
    ‘price’ => ‘required|numeric’,
    ‘quantity’ => ‘required|numeric’,
    ‘artwork_dimensions’ => ‘required|string’,
    $messages = [];
    if ($request->input(‘artwork_dimensions’) == ‘orientation_portrait’) {
    $rules[‘image’] = [‘required’, ‘image’, ‘mimes:jpeg,png,jpg,gif,svg’, ‘dimensions:min_width=1024,min_height=512,max_width=1080,max_height=1920’, ‘max:500000’];
    $messages = [‘dimensions’ =>’Image require minimum (1024×512). or maximum (1080×1920) dimension’];
    if ($request->input(‘artwork_dimensions’) == ‘orientation_landscape’) {
    $rules[‘image’] = [‘required’, ‘image’, ‘mimes:jpeg,png,jpg,gif,svg’, ‘dimensions:min_width=512,min_height=1024,max_width=1920,max_height=1080’, ‘max:500000’];
    $messages = [‘dimensions’ =>’Image require minimum (512×1024). or maximum (1920×1080) dimension’];
    if ($request->input(‘artwork_dimensions’) == ‘orientation_square’) {
    $rules[‘image’] = [‘required’, ‘image’, ‘mimes:jpeg,png,jpg,gif,svg’, ‘dimensions:min_width=1024,min_height=1024,max_width=1920,max_height=1920’, ‘max:500000’];
    $messages = [‘dimensions’ =>’Image require minimum (1024×1024). or maximum (1920×1920) dimension’];
    $validator = Validator::make($request->all(), $rules, $messages);
    if ($validator->fails()) {
    return $this->error(‘Validation Failed’, [‘errors’ => $validator->errors()]);

    • you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the sometimes rule to your rule list:

      $v = Validator::make($request->all(), [
      ‘portrat’ => ‘sometimes|required|…|…’,
      ‘squar’ => ‘sometimes|required|..|..’,
      ‘landscape’ => ‘sometimes|required|..|.’,
      In the example above, the portrait field will only be validated if it is present in the $data array.

  3. Is there a method to read the allowed maximum file size value in Laravel?

    (other than calling ini_get(‘upload_max_filesize’))


Please enter your comment!
Please enter your name here