PostTooLargeException in ValidatePostSize.php line

2019-04-29 07:42发布

问题:

I am trying to upload a form containing images too. When I submit it. it shows this error

PostTooLargeException in ValidatePostSize.php line 22:

How do I resolve this issue?

回答1:

Check the following parameters in your php.ini file.

I've had this issue on several occasions and it's usually because the max_file_size is set to 2M by default.

  • max_file_size
  • upload_max_filesize
  • post_max_size

**Edit I was asked how to validate file size using the validate method in Laravel before the file is sent to PHP and alerting the user of the large file. You can and this is how:

1. Create an error alert for the screen This is what mine looks like. I use bootstrap 3 style. Add something like this into your layout (it will only appear if you have an error).

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br> 
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
        </div>
@endif

2. Identify the validator you will use within the pre-canned validation classes ** Go to your project/Resources/lang/en/validators.php You'll find all the validations available in laravel. You'll see they have this on in there:

'max'                  => [
    'numeric' => 'The :attribute may not be greater than :max.',
    'file'    => 'The :attribute may not be greater than :max kilobytes.',
    'string'  => 'The :attribute may not be greater than :max characters.',
    'array'   => 'The :attribute may not have more than :max items.',
],

This is the validation rule I used to check file size.

**4. Create your request file **

php artisan make:request yourRequest

**5. update your request file ** Go to yourProject/app/Http/Requests/yourRequest.php and add the following in the rules method 'file_name' => 'max:10' update 10 to the value of your limit in kilobytes:

    public function rules()
    {
        return [ 'profile_pic' => 'required|image|mimes:jpg|max:10',
        ];
    }

6. Make your Request with whichever the request file is managing your rules. So in this scenario we named it yourRequest, so your save method would have:

public function upload(Requests\yourRequest $request)

Also make sure your Controller uses the requests class in it like so:

use App\Http\Requests;

If you follow this you'll have an error that looks like this:

The other option is to shrink the file (in the case of an image) using the Image class.



回答2:

You should use client-side validation on upload file and if there are multiple files to upload options you should insert data through of javascript or one by one

Example code:

 $(document).on("change", "#imageUploader", function(e) {
      if(this.files[0].size > 7244183)
      {
        alert("The file size is too larage"); 
        document.getElementById('#imageUploader').value = "";
      }
});