Monday, 15 March 2010

php - symfony huge file download -


I have a controller in Symfony2, which provides file downloads.

Gt; Receive ('principle') - & gt; GetManager (); $ Multilateral = $ em-> GetRepository ('AppBundle: polynomial') - & gt; FindById ($ polynomialId); $ Filename = $ polynomial-> GetFileLocation (); $ Response = new response (); $ Reaction-> Header-> Set ('cash-control', 'private'); $ Response- & gt; Header-> Set ('content-type', mime_content_type ($ filename)); $ Response- & gt; Header-> Set ('content-dispute', 'attachment; filename =' '. Basnem ($ filename).' ''; '); $ Response- & gt; Header-> Set ('content-length', filesystem ($ filename)); $ Response & gt; SendHeaders (); $ Response & gt; SetContent (readfile ($ filename)); Return $ $- & gt; Redirect ($ this- & generationalUrl ('users_polynomials')); }

This job is fine for small files (& lt; 100MB). But if I want to download more than 100 MB file I get an error (ERR_INVALID_RESPONSE) I found that this problem is probably in the PHP function readfile () , which causes memory exhaustion.

Is there any way in Symphony 2.6.1 to download large files without memory exhaustion?

Sure you want to use Symfony \ Component \ HttpFoundation \ BinaryFileResponse .

$ response = New BinaryFileResponse ($ filename);

This will also automatically take care of the headers for you (although you can add more headers if needed).


No comments:

Post a Comment