mathishuettl's avatar

Handle PostTooLargeException

Hello,

how can it be done that when I try to upload a file that is bigger than my post_max_size that the controller handles this error? It doesn't look very nice for the user when this error gets thrown.

I already have a validation rule for my upload:

"required|file|mimes:jpeg,jpg,png,webm,mp4|max:1024";

But when I select a 300mb file as example then the controller should validate this not laravel itself. You know what i mean?

I just want to reaturn to the previous page and show an error alert.

0 likes
3 replies
hajrovica's avatar

Hmm i am not sure you can go this way about it

process of file upload is:

  1. that you point your form to file and initiate upload
  2. server finishes upload of file
  3. then your logic of form validation kicks in and determines if file is to large or not

and for you when uploading 300 MB file following occurs after point 1.:

  • server starts upload,
  • after max upload file size is reached or php time execution script is reached your server stops and breaks request
  • server displays error

So in fact if you want to check file size before you initiated upload. Try exploring some JS or HTML5 Api alternatives for this

Try to google it for example;

Client Checking file size using HTML5?

Validating File Size Before Form Submit (HTML5)

JavaScript file upload size validation

Tray2's avatar

Checking the size is easy

let fileUpload = document.querySelector("#fileUpload");
        if (typeof (fileUpload.files) != "undefined") {
            let size = parseFloat(fileUpload.files[0].size / 1024).toFixed(2);
            alert(size + " KB.");

Btw don't highjack a thread that is several years old,

Please or to participate in this conversation.