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:
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.1Gfor a 2GB file)upload_max_filesize: Set directly to2Gmemory_limit: You don’t need 2GB here!256Mis enough if you use chunked uploads (we’ll get to that)max_execution_time: Bump to300(5 minutes) to account for slow upload speedsmax_input_time: Also set to300or higher
Don’t forget to restart your web server (Apache/Nginx) and PHP-FPM—changes won’t kick in otherwise.
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>
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]); }
- Nginx: Add
client_max_body_size 2.1G;to your site config ornginx.conf - Apache: Add
LimitRequestBody 2147483648(2GB in bytes) to your.htaccessor virtual host settings
- 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

