Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - validate file size when PHP max upload size limit is exceeded

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?

like image 737
baig772 Avatar asked Sep 06 '17 05:09

baig772


3 Answers

post_max_size and upload_max_filesize are directives you can set to configure your php.ini file OR .htaccess file OR httpd.conf file.

php.ini example:

post_max_size=15M
upload_max_filesize=15M

.htaccess example:

php_value post_max_size=15M
php_value upload_max_filesize=15M

httpd.conf example:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/html/example/
    ErrorLog /var/log/httpd/error.log
    <Directory /var/www/html/example/>
      AllowOverride None
      <IfModule mod_php5.c>
        php_value post_max_size=15M
        php_value upload_max_filesize=15M
      </IfModule>
    </Directory>
</VirtualHost>
like image 164
AddWeb Solution Pvt Ltd Avatar answered Nov 17 '22 07:11

AddWeb Solution Pvt Ltd


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.)

like image 40
patricus Avatar answered Nov 17 '22 09:11

patricus


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

in your controller

and in php.ini

upload_max_filesize = 10MB
like image 10
Exprator Avatar answered Nov 17 '22 08:11

Exprator