Laravel max upload size limitations

2019-01-19 02:56发布

I have my website hosted at a shared server. The maximum upload limit is 5MB. All the validations and file uploading works fine if the uploaded file is under 5MB. But when a file greater than 5MB is uploaded, i see this enter image description here. How can I validate or force the file to be under the upload limit from server?

8条回答
爷、活的狠高调
2楼-- · 2019-01-19 03:05
$validator = Validator::make($request->all(), [
    'file' => 'max:5120', //5MB 
]);

in your controller

and in php.ini

upload_max_filesize = 10MB
查看更多
闹够了就滚
3楼-- · 2019-01-19 03:09

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.

查看更多
欢心
4楼-- · 2019-01-19 03:16

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>
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-19 03:18

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

查看更多
Juvenile、少年°
6楼-- · 2019-01-19 03:18

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.

查看更多
等我变得足够好
7楼-- · 2019-01-19 03:19

you can check size of your file by max validator

'file' => 'required|max:5128'; // max file size: 5M
查看更多
登录 后发表回答