0

I'm trying to handle PostTooLargeException in laravel 9 without updating php.ini file.

In app/Exceptions/Hndler.php I have call below renderable function

use Illuminate\Http\Exceptions\PostTooLargeException as PostTooLargeException;

public function register()
{
    $this->renderable(function (PostTooLargeException $e, $request) {
        return response()->view('errors.custom', [], 500);
    });
}

Controller add action

public function add( \App\Http\Requests\AuthorFormRequest $request )
{
        dd($request)
}

AuthorFormRequest Is like below

public function rules()
{
        return [
            'user_icon' => 'image|mimes:jpg,png,jpeg,gif,svg|max:2048|dimensions:min_width=100,min_height=100',
            'name' => 'required|string|max:30',
            'group' => 'nullable|string|max:30',
            'position' => 'nullable|string|max:30',
            'category' => 'nullable|string|max:30',
            'description' => 'nullable|string|max:200',
            'stripe_id' => 'email',
            'image_tmp' => 'nullable|string'
        ];
}

Every time I'm getting below image if image size is 1gb.

enter image description here

My expectation is display the error message as a session message in upload page. How will handel this error ?

Niloy Rony
  • 602
  • 1
  • 8
  • 23
  • Add the piece of code from your upload page, the try/catch will do for now. Are you using form requests to validate the post size? – UnderDog Dec 24 '22 at 02:03
  • Ok, you're getting close to your solution. But first: "Every time I'm getting below image if image size is 1gb." 1gb for a user_icon image? That's a bit big, 37mb is a bit big as well. 2mb is too big, but in your validation rules, you're saying it correctly: my file cannot be bigger than 2mb. This time, you need to edit your nginx config: https://www.tecmint.com/limit-file-upload-size-in-nginx/ https://docs.rackspace.com/support/how-to/limit-file-upload-size-in-nginx/ in the meantime, test it out with an image, smaller than 8mb – UnderDog Dec 24 '22 at 08:03
  • @UnderDog I want to handle it in laravel end. Because user_icon just an example .In future I want to implement it for big file example book file. – Niloy Rony Dec 24 '22 at 08:41
  • Understood, but your server cannot handle an update larger than 8mb, so you need to edit your nginx file _first_. Your upload does not reach the _validator_, because the server cannot handle the file – UnderDog Dec 24 '22 at 09:04
  • Understood but In laravel why this PostTooLargeException class ? https://laravel.com/api/9.x/Illuminate/Http/Exceptions/PostTooLargeException.html – Niloy Rony Dec 24 '22 at 09:08
  • You're right about that one. So... can you post your laravel.log? Post it inside code tags, like you did with your code examples – UnderDog Dec 24 '22 at 09:19
  • Does this answer your question? [PostTooLargeException in Laravel](https://stackoverflow.com/questions/43464593/posttoolargeexception-in-laravel) – steven7mwesigwa Dec 24 '22 at 18:04
  • [How to catch PostTooLargeException in Laravel?](https://stackoverflow.com/questions/42883154/how-to-catch-posttoolargeexception-in-laravel) – steven7mwesigwa Dec 24 '22 at 18:08
  • @steven7mwesigwa render has deprecated in version 9 , that's why I already used register and it not working, and your first comment I already mention I don't want to handle it by intl. – Niloy Rony Dec 26 '22 at 03:12
  • You seem to be facing a `419` error as well since that is what your application is handling. Check out: [Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired](https://stackoverflow.com/questions/52583886/post-request-in-laravel-error-419-sorry-your-session-419-your-page-has-exp) and [419 Page Expired In Laravel Even after adding CSRF token](https://stackoverflow.com/questions/71540756/419-page-expired-in-laravel-even-after-adding-csrf-token) and [page expired exception in laravel?](https://stackoverflow.com/questions/49864923/page-expired-exception-in-laravel) – steven7mwesigwa Dec 26 '22 at 07:57

0 Answers0