Ngozistephen's avatar

413 Request Entity Too Large

I am uploading files to cloudinary with this code

  public function upload(Request $request)
    {
        $folder = 'product_photo';
    
        if ($request->hasFile('photo')) {
            $file = $request->file('photo');
            request()->validate([
                'photo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:6048',
               
            ]);
    
            $cloudinaryResponse = Cloudinary::upload($file->getRealPath(), [
                'folder' => $folder,
            ]);
    
            $secureUrl = $cloudinaryResponse->getSecurePath();
            return response()->json(['secure_url' => $secureUrl], 200);
        } else {
            return response()->json(['error' => 'No file uploaded'], 400);
        }
    }

the problem is that it works on local but on production it shows this error message

<html>

<head>
    <title>413 Request Entity Too Large</title>
</head>

<body>
    <center>
        <h1>413 Request Entity Too Large</h1>
    </center>
    <hr>
    <center>nginx/1.24.0</center>
</body>

</html>

my laravel application is hosted on railway. Please how can i solve this problem

0 likes
7 replies
jlrdw's avatar

What is the limit that's set. And try chunking.

Ngozistephen's avatar

@jlrdw the fie i am uploading is 5 MB and in the code the validation is not maximum of 6 MB

Tray2's avatar

@Ngozistephen It's not the validation that fails, php have a default limit of 2Mb for upload, make sure that you change that in your php.ini file and restart the server. You need to change the max allowed post size as well.

1 like
Ngozistephen's avatar

@Tray2 my post_max_size = 2G

the problem is that the file is uploaded to cloudinary on local but on production it shows 413 error

martinbean's avatar

@Ngozistephen You can’t just upload files that are multiple megabytes or even gigabytes in size. You’re either going to hit a request limit or memory limit.

As @jlrdw has suggested, if you’re going to upload larger files (more than a couple of megabytes in size) then you need to send the file in chunks, otherwise you’re going to get 413 errors like you have, or memory exceeded errors.

1 like

Please or to participate in this conversation.