News Archive
PhpRiot Newsletter
Your Email Address:

More information

readfile() not considered harmful

Note: This article was originally published at Planet PHP on 4 May 2012.
Planet PHP

If you're like me, you've probably read a dozen or two articles about PHP performance in your career. Many of them are quite good, but some are simply flat out wrong, or misinformed.

One of the old truisms that has been repeated for as long as I can recall is "don't use readfile() if you have big files, because it reads the whole file into memory and your server will explode." The usual advice is to manually stream a file, like so:

$fp = fopen('bigfile.tar', 'rb');
while (!feof($fp)) {
print fread($fp, 1024);

There's just one problem with that age-old truism: It's not true.

read more