I'm trying to download a file when a user clicks on download link.
In Controller:
$response = new Response();
$response->headers->set('Content-type', 'application/octect-stream');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
$response->headers->set('Content-Length', filesize($filename));
return $response;
This is opening the dialog box to save the file, but it says the file is 0 bytes. And changing it to:
$response = new Response();
$response->headers->set('Content-type', 'application/octect-stream');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
$response->headers->set('Content-Length', filesize($filename));
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->setContent(readfile($filename));
return $response;
I get a bunch of weird characters instead of the file download dialog box.
Finally, switching the "setContent" line to:
$response->setContent(file_get_contents($filename));
It returns a PHP error:
Fatal error: Allowed memory size...
Any clues on how to achieve this? I've done it before in PHP (wihtout MVC), but I don't know what can be missing to do it through Symfony2...
Maybe the solution is setting the memory_limit in PHP.INI, but I guess it´s not the best practice...
The most comfortable solution is
First of all, thanks to everyone for your replies. I finally solved this without X-SendFile (which is probably the best practice). Anyway, for those who can't get X-Sendfile apache module to work (shared hosting), here's a solution:
Hope this helps!
+1 for alexander response.
But if you can't use X-Sendfile, you should use the BinaryFileResponse added in the 2.2: http://symfony.com/doc/current/components/http_foundation/introduction.html#serving-files
In my project the result is
For those who don't have the option of setting headers:
The
download
attribute may help depending on which browsers you need to support:<a href="{file url}" download>
or
<a href="{file url}" download="{a different file name}">
This is not supported in all legacy browsers. See this page for browser support:
https://www.w3schools.com/tags/att_a_download.asp
Don't know if it can help but it's
application/octet-stream
notapplication/octect-stream
As of Symfony 3.2 you can use the file() controller helper which is a shortcut for creating a
BinaryFileResponse
as mentioned in a previous answer: