You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel视频上传触发Post Too Large异常,修改php.ini无效求解决

Hey there! Let's tackle two things here: implementing video upload and storage in Laravel, and fixing that frustrating "Post Too Large" exception you're dealing with even after editing php.ini.

Implementing Video Upload & Storage in Laravel

Step 1: Configure Filesystem Settings

First, let's make sure your filesystem is set up to handle video files. Open config/filesystems.php and check the disks array. For local storage, the public disk is a great choice since it makes files web-accessible:

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

After configuring, run this command to create a symbolic link between storage/app/public and public/storage (so your videos are accessible via the web):

php artisan storage:link

Step 2: Build the Upload Form

Create a Blade template with a form that supports file uploads. Don't forget the enctype="multipart/form-data" attribute—this is critical for uploading files:

<form action="{{ route('videos.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div>
        <label for="video">Upload Video:</label>
        <input type="file" name="video" id="video" accept="video/*">
        @error('video')
            <p class="text-red-500">{{ $message }}</p>
        @enderror
    </div>
    <button type="submit">Upload</button>
</form>

Step 3: Handle the Upload in Your Controller

Create a controller method to validate and store the video. Add validation rules to restrict file types and size (we'll align this with your PHP config later):

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

public function store(Request $request)
{
    // Validate the uploaded file
    $validated = $request->validate([
        'video' => 'required|file|mimes:mp4,mov,avi,mkv|max:204800', // 200MB in KB
    ]);

    // Store the video in the public disk's "videos" directory
    $videoPath = $request->file('video')->store('videos', 'public');

    // Optional: Store the path in your database
    // Video::create(['path' => $videoPath]);

    return redirect()->back()->with('success', 'Video uploaded successfully!');
}

You can also use storeAs if you want to set a custom filename instead of the auto-generated one:

$filename = uniqid().'.'.$request->file('video')->getClientOriginalExtension();
$videoPath = $request->file('video')->storeAs('videos', $filename, 'public');

Step 4: Access the Uploaded Video

To display or link to the video, use the Storage::url() method or the asset() helper:

<!-- Using Storage facade -->
<video controls>
    <source src="{{ Storage::url($video->path) }}" type="video/mp4">
    Your browser does not support the video tag.
</video>

<!-- Or using asset helper -->
<video controls>
    <source src="{{ asset('storage/'.$video->path) }}" type="video/mp4">
    Your browser does not support the video tag.
</video>
Fixing the "Post Too Large" Exception

If you've edited php.ini but still see this error, here's what to check:

1. Verify You're Editing the Correct php.ini File

It's common to edit the wrong php.ini (PHP has separate configs for CLI and web). To confirm which one your web server uses:

  • Create a temporary route in routes/web.php:
    Route::get('/phpinfo', function () {
        phpinfo();
    });
    
  • Visit /phpinfo in your browser, look for the Loaded Configuration File line—this is the php.ini you need to edit.

2. Update Key PHP Configuration Parameters

Edit that php.ini file and update these values (adjust sizes based on your needs; example uses 200MB):

post_max_size = 200M
upload_max_filesize = 200M
max_execution_time = 300 ; 5 minutes, since large videos take time to upload
max_input_time = 300
memory_limit = 256M

3. Restart Your Web Server & PHP-FPM

Changes to php.ini won't take effect until you restart your web server and PHP-FPM (if you're using it):

  • For Apache:
    sudo systemctl restart apache2
    
  • For Nginx + PHP-FPM:
    sudo systemctl restart nginx
    sudo systemctl restart php8.2-fpm ; adjust version to match your PHP
    

4. Check Nginx/Apache Server Limits

Nginx has its own limit for request body size that overrides PHP settings. Open your Nginx site config (usually in /etc/nginx/sites-available/your-site) and add:

server {
    # ... other configs
    client_max_body_size 200M; ; Match this to your post_max_size
}

Restart Nginx after making this change. For Apache, you might need to check LimitRequestBody in your site config or httpd.conf—set it to 209715200 (200MB in bytes) if needed.

5. Align Laravel Validation Rules with PHP Config

Make sure the max value in your Laravel validation rule matches your PHP upload_max_filesize. For example, if PHP is set to 200MB, the rule should be max:204800 (since Laravel uses KB for file size validation: 200 * 1024 = 204800).


内容的提问来源于stack exchange,提问作者Bhushan Gosavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:31:50