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

Symfony3网站最大2GB大文件上传问题求助

Hey there! Uploading 2GB files in Symfony 3 needs some special care—standard upload methods load the entire file into memory, which is why you’re hitting limits even after tweaking php.ini. Let’s walk through the fix step by step:

Step 1: Lock Down All Critical PHP Configs

You probably missed a few key settings in php.ini (or use .htaccess/user.ini if you don’t have server access). Here’s what you need to set:

  • post_max_size: Must be larger than your upload limit (e.g., 2.1G for a 2GB file)
  • upload_max_filesize: Set directly to 2G
  • memory_limit: You don’t need 2GB here! 256M is enough if you use chunked uploads (we’ll get to that)
  • max_execution_time: Bump to 300 (5 minutes) to account for slow upload speeds
  • max_input_time: Also set to 300 or higher

Don’t forget to restart your web server (Apache/Nginx) and PHP-FPM—changes won’t kick in otherwise.

Step 2: Use Chunked Uploads (Ditch FileManagerBundle for This)

FileManagerBundle isn’t built for massive files. Instead, use OneupUploaderBundle—it’s made specifically for chunked uploads and works perfectly with Symfony 3.

First, install it via Composer:

composer require oneup/uploader-bundle

Then configure it in app/config/config.yml:

oneup_uploader:
    mappings:
        large_file_upload:
            frontend: blueimp # Choose from blueimp, dropzone, or fineuploader
            storage:
                directory: "%kernel.root_dir%/../web/uploads/large_files"
            chunking:
                enabled: true
                max_size: "100M" # Size of each chunk—adjust based on your server's comfort zone

Add the routing in app/config/routing.yml:

oneup_uploader:
    resource: .
    type: oneup_uploader

Set up the frontend (using Blueimp as an example—super straightforward):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="/bundles/oneupuploder/jquery.fileupload.js"></script>
<script>
$(function () {
    $('#large-file-upload').fileupload({
        url: '{{ oneup_uploader_endpoint('large_file_upload') }}',
        dataType: 'json',
        maxFileSize: 2097152000, // 2GB in bytes
        chunked: true,
        chunkSize: 104857600, // 100MB chunks (matches our bundle config)
    });
});
</script>

<input id="large-file-upload" type="file" name="files[]" multiple>
Step 3: Process Files Without Loading Them Into Memory

If you need to validate or move the uploaded file, avoid functions like file_get_contents() that load everything into RAM. Use Symfony’s SplFileObject instead—it lets you work with the file in streams:

public function handleUploadAction(Request $request)
{
    $uploadedFile = $request->files->get('files')[0];
    $streamedFile = $uploadedFile->openFile();

    // Example: Copy the file to a final location (streamed, no memory bloat)
    $targetPath = $this->getParameter('kernel.root_dir') . '/../web/uploads/final/' . $uploadedFile->getClientOriginalName();
    $streamedFile->copy($targetPath);

    // Or process line by line if you need to parse content
    foreach ($streamedFile as $line) {
        // Do your processing here
    }

    return new JsonResponse(['success' => true]);
}
Step 4: Fix Web Server Limits (Don’t Forget These!)
  • Nginx: Add client_max_body_size 2.1G; to your site config or nginx.conf
  • Apache: Add LimitRequestBody 2147483648 (2GB in bytes) to your .htaccess or virtual host settings
Quick Troubleshooting
  • Still seeing memory errors? Double-check that no part of your code is reading the entire file into memory. Stick to stream-based operations.
  • FileManagerBundle issues? It’s not designed for chunked uploads—save it for small file management tasks instead.

内容的提问来源于stack exchange,提问作者F. Zabunov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:16:42