Laravel validation rule for max filesize does not work

Viewed 644

enter image description hereIn my laravel project, I have this snippet of my validator as

$this->validate($request,
    ['name'=>'required|unique:activities,name,'.$request->id,
    'images.*'=>'image|max:65536',
    'description'=>'required'
    ]
    );

I want a maximum of 64 MB of images to be uploaded at a time. I have also set the max_upload_size and max_post_size equal to 64 MB in my php.ini file and also edited max_file_uploads to a safe high value. Isn't it supposed to return a error message and redirect me to the previous message when I try to upload files above 64 MB?? But I get a message of PostTooLargeException with

    public function handle($request, Closure $next)
{
    $max = $this->getPostMaxSize();

    if ($max > 0 && $request->server('CONTENT_LENGTH') > $max) {
        throw new PostTooLargeException;
    }

    return $next($request);
}

Why is this happening?? Most answers direct me to edit php.ini file which I already did. How do I stop this from happening and safely redirect the user to the previous page with validation error messages??

1 Answers

The reason you get such output is because the error has occurred before control reaches your action, hence you can't do anything about it in your Controller.

But if you really want to catch this exception, you can add your custom exception handling code in render() method of Handler.php which is available in app/Exceptions folder.

I've myself written such code, For example:

if ($exception instanceof \Illuminate\Session\TokenMismatchException) {

Instead of \Illuminate\Session\TokenMismatchException, you'll have to specify full qualified path to PostTooLargeException class. $exception is an argument to the render() method specified earlier.

Related