Reputation: 622
I am trying to return a PDF to the browser using Symfony 2, so once I have located the file, I use:
return new Response(readfile($file_path), 200, array(
'Content-Type' => 'application/pdf'
));
but if I load the page, headers are not being modified and the content is not being interpreted as a pdf...
< HTTP/1.1 200 OK
< Date: Sat, 31 Mar 2012 20:39:20 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.6
< Set-Cookie: PHPSESSID=XXXX; path=/
< Transfer-Encoding: chunked
< Content-Type: text/html
I'm lost with this problem. Any thoughts?
Upvotes: 1
Views: 1275
Reputation: 3370
You may also want to use the following
new StreamedResponse(function () use ($file) {
readfile($file);
}, 200, array('Content-Type' => 'image/png');
Upvotes: 1
Reputation: 4894
readfile
executes first, and starts sending the file to the client. This triggers the headers to be generated. Then the return value of readfile is passed to Response
. When Response is returned to the client it's impossible for PHP to change the headers because they were triggered when readfile ran.
Replace readfile
with file_get_contents
.
Upvotes: 7