DaiBu DaiBu - 10 months ago 28
PHP Question

What overhead is caused by echo + fread-ing a file for download?

If I have a php script that does nothing but the following...

$file = fopen($path, "r");
flock($file, LOCK_SH);

echo fread($file, filesize($path));

flock($file, LOCK_UN);

...how much overhead is caused by accessing the script from a browser as opposed to simply accessing the actual file? Internally, is the entire file copied to some kind of buffer and then spit out again or is it nearly the same thing?


Yes, it's read into a buffer. It's essentially equivalent to:

$temp = fread($file, filesize($path));
echo $temp;

You can use fpassthru() to send to the client without reading everything into a buffer at once.