note 102759 deleted from function.readfile by danbrown
| From: | danbrown@php.net | Date: | Fri, 04 Mar 2011 14:53:10 +0000 |
| Subject: | note 102759 deleted from function.readfile by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177562@lists.php.net to get a copy of this message | ||
Note Submitter: chrisbloom7 at gmail dot com
Reason: integrated
----
A note about my previous comment posted on 16-Aug-2010:
I started running into trouble when I had really large files being sent to clients with really slow
download speeds. In those cases, the script would time out and the download would terminate with an
incomplete file. I am dead-set against disabling script timeouts - any time that is the solution to
a programming problem, you are doing something wrong - so I attempted to scale the timeout based on
the size of the file. That ultimately failed though because it was impossible to predict the speed
at which the end user would be downloading the file at, so it was really just a best guess so
inevitably we still get reports of script timeouts.
Then I stumbled across a fantastic Apache module called mod_xsendfile
(https://tn123.org/mod_xsendfile/ (binaries) or https://github.com/nmaier/mod_xsendfile
(source)). This module basically monitors the output buffer for the presence of special headers, and
when it finds them it triggers apache to send the file on its own, almost as if the user requested
the file directly. PHP processing is halted at that point, so no timeout errors regardless of the
size of the file or the download speed of the client. And the end client gets the full benefits of
Apache sending the file, such as an accurate file size report and download status bar.
The code I finally ended up with is too long to post here, but in general is uses the mod_xsendfile
module if it is present, and if not the script falls back to using the code I originally posted. You
can find some example code at https://gist.github.com/854168