Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache / PHP output caching

I'm working on a PHP script which generates large (multi-MB) output on the fly without knowing the length in advance. I am writing directly to php://output via fwrite() and have tried both standard output and using Transfer-Encoding: chunked (encoding the chunks as required) but no matter what I try the browser waits until all the data is written before displaying a download dialog. I have tried flush()ing too after the headers and after each chunk but this also makes no difference.

I'm guessing that Apache is caching the output as the browser would normally display after receiving a few kB from the server.

Does anyone have any ideas on how to stop this caching and flush the data to the browser as it is generated?

Thanks, J

like image 638
JWood Avatar asked Aug 13 '10 09:08

JWood


1 Answers

First of all, like BlaM mentioned in his comment, if in the PHP configuration OutputBuffering is enabled, it wont work, so it would be useful to know your phpinfo().

Next thing, try if it works with a big file that is stored on yor webserver, output it usinf readfile. And, together with this, check if you send the correct headers. Hints on how to readfile() and send the correct headers a provided here: StackOverflow: How to force a file download in PHP

And while you are at it, call ob_end_flush() or ob_end_clean() at the top of your script.

like image 199
ThE_-_BliZZarD Avatar answered Oct 03 '22 02:10

ThE_-_BliZZarD