Upload file "413 - content too large"

Error Message

The server returned a “413 Content Too Large”.

Other Information

I have a form to upload videos, the videos I upload are directly saved to YouTube without saving them to the database. Only the title, description, and YouTube ID attributes are saved to the database, but why do I get the error message “The server returned “413 Content Too Large”.”

how to fix it?

public function store(Request $request) { 
        $apiKey = env('GOOGLE_API_KEY');
        $user = Auth::user();
        $googleToken = $user->google_token;

        $client = new Client();
        $client->setAuthConfig(base_path('google-oauth-client.json'));
        $client->setDeveloperKey($apiKey);
        $client->setRedirectUri(env('GOOGLE_REDIRECT_URI'));
        $client->addScope(YouTube::YOUTUBE_UPLOAD, Youtube::YOUTUBE);
        $client->setAccessType('offline');
        $client->setApprovalPrompt('force');

        $client->setAccessToken([
            'access_token' => $googleToken,
            'expires_in' => 7200,
        ]);

        $request->validate(
        [
            'title' => 'required',
            'description' => 'required',
            'video_url' => 'file|mimetypes:video/mp4,video/avi,video/mkv|max:150000', // Maks 150MB
        ],
        [
            'title.required'=> 'Title is required',
            'description.required'=> 'description is required',
            'video_url.mimetypes' => 'Video formats only MP4, AVI, or MKV',
            'video_url.max' => 'Maximum video size is 150MB',
        ]

        $service = new Youtube($client);
        $snippet = new VideoSnippet();
        $status = new VideoStatus();
        $video_youtube = new Video();

        $snippet->setTitle($request->title);
        $snippet->setDescription(Str::of($request->description)->stripTags());
        $status->setPrivacyStatus('unlisted');
        $video_youtube->setSnippet($snippet);
        $video_youtube->setStatus($status);

        $video_file = $request->file('video_url');
        $video_path = file_get_contents($video_file->getPathname());
        $video_mime = $video_file->getMimeType();

        $insertRequest = $service->videos->insert('snippet,status', $video_youtube, ['data' => $video_path, 'mimeType' => $video_mime]);

        $dataStore = [
            'title'=> $request->title,
            'slug'=> $this->generateSlug($request->title),
            'description'=> $request->descr,
            'youtube_video_id' => isset($insertRequest->id) ? $insertRequest->getId() : null
            'user_id' => $user->id,
        ];

        Post::create($dataStore);
        
        return redirect()->route('admin.blogs.index')->with('success', 'successful adding data');
    }

I’m not sure how the API ect that you’re using works, but based on the error you’re reciving, I’d guess that the video is technically being uploaded to your site and then being proccessed.

If this is the case then if the video is over 10 MB, then it’ll fall over.

I might be wrong, but thats what it looks like to me.

6 Likes

There is a file size limit for files that are uploaded to our servers. Even though you don’t store the files on our servers in the end, they still need to be transferred to our server first before you can upload them to YouTube (if you’re uploading them through PHP, which you appear to be doing). And there are limits on that.

If you can upload the video directly from the visitor’s browser onto YouTube without hitting our servers first, that would let you work around this. If not, then you may need to upload the files to YouTube yourself and then link then in your website afterward.

4 Likes