nine's avatar
Level 1

best pratice to handle PostTooLargeException

Hi,

i'm facing this exception and ask for the best way to manage it.

I have this apache configuration :

upload_max_filesize => 5M => 5M post_max_size => 8M => 8M

and this rule in UploadRequest : 'image|mimes:jpeg,bmp,png|size:5000'

I read a lot about this question and found this solution that consist to catch the exception :

     if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException) {
         return response()->view('errors.posttoolarge');
     }

What i'm looking for is not to redirect the user to this page. Because, i already inform him (mention in uplaod Form) of Max size (less than 5 MO). What I want to do is use intervention image and resize image to be less than Apache pre-requises .

So, finally what 's the best practice : should i have to write some function in Exception/Handler to resize and save image uploaded. And redirect then ?

thank you

0 likes
9 replies
nine's avatar
Level 1

thank you for reply.

so i have to check in JS before.

Is there anyway to make this in php ?

In Handler file render method there is two parameters request and exception but no form values in this request, if i check :

dd($request->photos) : it returns null

Have you an idea ? Thank you again.

Snapey's avatar

if you want to resize in php then you will just have to permit even larger posts by increasing the max upload and the max post size

nine's avatar
Level 1

ok, but i can't update those values. the wandered solution is to catch + resize in php (+ JS why not) . I'm still looking for a solution. if there is ;-)

thanks

Snapey's avatar

if the file is truly too big then the webserver will just not accept it, so it will never get to your code or the laravel framework.

If you get the postTooLarge exception the the webserver probably does not have the upload to resize. Its never going to get to your code because it was too big.

Imagine you ordered a very large piece of furniture for your home. At the door the delivery man says it just won't fit through the door. You say, ok, bring it in and we'll just cut some off.... doh!

lostdreamer_nl's avatar

Try out dropzone.js: http://www.dropzonejs.com/

With it you can upload 1 or multiple files (drag + drop or regular file upload) but it has settings to check for max filesize and max width / height. It uses canvas + js to resize your image before uploading if it's too big.

I've used it in projects where photographers will upload raw images (50 MB+, 10-20MP) and by the time they hit the server they will be 4K resolution < 5MB.

Wonderful library if you ask me.

nine's avatar
Level 1

yes, may be i understood

This is why i'm asking for catching the exception (a way to come back to my code) and then do the job (resize, save, redirect) in php

But after catching the exception i loose submitted informations . If there is a way to retreive them, i listen to you

Snapey's avatar

no. The exception will be thrown because the upload information is lost. It will not go through the door

nine's avatar
Level 1

ok, this is the answer i'm looking for. thank you @snapey.

so, i'll make it in JS . I'll try dropzone. Thank you @lostdreamer_nl

Have a good day

Please or to participate in this conversation.