19

I have my website hosted at a shared server. The maximum upload limit is 5MB. I do not have the ability to change the PHP ini settings, which is fine.

In Laravel, all the validations and file uploading works fine if the uploaded file is under 5MB. However, when a file greater than 5MB is uploaded, I get the following exception instead of a validation error:

FileException - The file "6MB_1.jpg" exceeds your upload_max_filesize ini directive (limit is 5120 KiB).

How can I validate or force the file to be under the upload limit from server?

patricus
  • 59,488
  • 15
  • 143
  • 145
baig772
  • 3,404
  • 11
  • 48
  • 93
  • 1
    May be You have to change your `upload_max_filesize` value in php.ini. Contact your hosting provider to change this if you have no permission to change ini file – Nazmul Hasan Sep 06 '17 at 05:25
  • Is there any other way? Without changing upload limit from server? – baig772 Sep 06 '17 at 05:26
  • add an htaccess in your resource folder .. then add this line .. `php_value upload_max_filesize xM` and `php_value post_max_size xM` where x = any number value .. eg: 100M = 100mb – Demonyowh Sep 06 '17 at 05:54
  • May be you can use .htaccess file to make changes: https://stackoverflow.com/a/2185500/386579 – shasi kanth Sep 24 '21 at 12:56

9 Answers9

14
$validator = Validator::make($request->all(), [
    'file' => 'max:5120', //5MB 
]);

in your controller

and in php.ini

upload_max_filesize = 10MB
Exprator
  • 26,992
  • 6
  • 47
  • 59
  • As explained, it works fine if the file is under 5MB, but if I upload the file that is 10MB of size and max upload limit on my server is 5MB, this will not work, it will throw an exception – baig772 Sep 06 '17 at 05:27
  • then you need to change the max_upload_size of apache to be greater than 5MB as the exception is coming ffrom apache and not laravel,check the edited ans @baig772 – Exprator Sep 06 '17 at 05:29
  • `post_max_size` also, since it is expected to go as POST data – Fr0zenFyr May 27 '23 at 11:04
10

You don't seem interested in changing the PHP limits to allow larger files. It looks to me like you want your max upload to be 5MB, and return a proper response if it is over that.

You can handle the FileException exception inside your exception handler at app/Exceptions/Handler.php. Update the render method to add in the code you need. For example, if you'd like to return a validation exception, you will need to handle the validation inside the exception handler for the FileException exception.

public function render($request, Exception $exception)
{
    if ($exception instanceof \Symfony\Component\HttpFoundation\File\Exception\FileException) {
        // create a validator and validate to throw a new ValidationException
        return Validator::make($request->all(), [
            'your_file_input' => 'required|file|size:5000',
        ])->validate();
    }

    return parent::render($request, $exception);
}

This is untested, but should give you the general idea.

You can also do client side validation via javascript, so that a file that is too large is never actually sent to your server, but javascript can be disabled or removed by the client, so it would be good to have nice server side handling set up.

For the client side validation, if you attach an event handler to the "change" event for the file input, you can check the file size using this.files[0].size, and perform any additional actions after that (disable form, remove uploaded file, etc.)

patricus
  • 59,488
  • 15
  • 143
  • 145
5

Change below Parameters in php.ini it works! for me.

upload_max_filesize = 50M
memory_limit = 512M
max_input_time = -1
max_execution_time = 0
vivek s vamja
  • 1,001
  • 10
  • 11
4

You can use Laravel validation rules:

 'image_file_input_name' => 'required|mimes:jpeg,bmp,png|size:5000',

size:value

The field under validation must have a size matching the given value. For string data, the value corresponds to the number of characters. For numeric data, the value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.

You can read more about validation and its usage.

Alankar More
  • 1,107
  • 2
  • 8
  • 26
2

you can check size of your file by max validator

'file' => 'required|max:5128'; // max file size: 5M
Farshad
  • 72
  • 7
1

enter image description here

The steps are as follows:

  • open your terminal [CTL + SHIFT t] Run the command below:

    $ php -i | grep php.i

The command above will print the path to the php.ini file that your server is using, in my case I got:

Configuration File (php.ini) Path => /etc/php/7.2/cli

Loaded Configuration File => /etc/php/7.2/cli/php.ini

Before now, you might have been editing /etc/php/7.2/apache2/php.ini thinking that would solve the problem but no, it does not!

Now run: $ sudo nano /etc/php/7.2/cli/php.ini

Our concern is the values of post_max_size, upload_max_filesize and memory_limit which by default has the value of 8M, 2M, and 128M respectively. Now you see why you could not upload more than 2MB sized file .

Next up, search for those variables and change their values to suit your need. While doing that, ensure that the sizes follow the same ratio as the default values, that is memory_limit should be greater followed by post_max_size and then upload_max_filesize. For example, in my case, I used the following values:

post_max_size = 6G
upload_max_filesize = 4G
memory_limit = 8G

Once you’re done changing the values to suit your need, save the changes with CTL Xthen type Y and hit ENTER.

Next up, restart apache: $ sudo /etc/init.d/apache2 restart

NOte - this work for apache server if are running the nginx then

$ sudo cp /etc/php.ini.default /etc/php.ini

Edit the values as stated above

brew services restart php@7.2 [ replace 7.2 with the version of php you’re using] That is it! Now you can upload large files within the range of the values you specified above without troubles .

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
Sandeep Kumar
  • 409
  • 4
  • 5
0

In laravel you can validate like this -

$validator = Validator::make($request->all(), [
    'file' => 'max:5120',
]);

The value is in kilobytes. i.e. max:10240 = max 10 MB. And in jquery -

binds to onchange event of your input field

$('#upFile').bind('change', function() {

  //this.files[0].size         gets the size of your file and then you can validate accourdingly...
  alert(this.files[0].size);

});

Hope this will help you.

Suniti Yadav
  • 393
  • 2
  • 8
0

You could do this with jQuery / javascript (Which means that the file will not be uploaded but you can check the file before it is sent to your PHP script)

$('#myFile').bind('change', function() {
  let filesize = this.files[0].size // On older browsers this can return NULL.
  let filesizeMB = (filesize / (1024*1024)).toFixed(2);

  if(filesizeMB <= 5) {
      // Allow the form to be submitted here.
  } else {
      // Don't allow submission of the form here.
  }

});

But always do serverside validation anyways. Incase you move to a stronger website and still want your website to work without any quirks.

$validator = Validator::make($request->all(), [
    'file' => 'max:5120', //5MB
]);

All of this is untested, but should work.

Classified
  • 560
  • 1
  • 7
  • 21
-1

The 5 mb limitation is set by your webhost and likely cannot be changed.

To upload files larger than the limit you will have to upload it in parts.

A laravel package that does so: https://github.com/jildertmiedema/laravel-plupload

Quezler
  • 2,376
  • 14
  • 29