DaiBu
DaiBu

Reputation: 529

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);
fclose($file);

...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?

Upvotes: 0

Views: 60

Answers (1)

Barmar
Barmar

Reputation: 781731

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.

fpassthru($file);

Upvotes: 1

Related Questions