readfile() or file_get_contents()?
Moderator: General Moderators
readfile() or file_get_contents()?
I've figured out that I'm going to use a centralized script to handle all image requests. The script will pull images based on $_GET paramaters from different directories.
Now, this script could be being called hundreds of time per minute from hotlinking of images (think image hosting type site).
Which would be more friendly to my server. readfile() or file_get_contents()? Or, would it really even matter?
Now, this script could be being called hundreds of time per minute from hotlinking of images (think image hosting type site).
Which would be more friendly to my server. readfile() or file_get_contents()? Or, would it really even matter?
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: readfile() or file_get_contents()?
readfile() .scottayy wrote:I've figured out that I'm going to use a centralized script to handle all image requests. The script will pull images based on $_GET paramaters from different directories.
Now, this script could be being called hundreds of time per minute from hotlinking of images (think image hosting type site).
Which would be more friendly to my server. readfile() or file_get_contents()? Or, would it really even matter?
file_get_contents() has the overhead of creating a PHP string as well as allocating memory to put that string in.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Yeah I thought that too, which is why I retracted my last assertion but then there's something in the back of my brain telling me it's file_get_contents() or maybe that could be file_get_contents() versus fopen().file_get_contents() has the overhead of creating a PHP string as well as allocating memory to put that string in.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Using fopen/fread and placing the contents of a file into a PHP string is likely to be less efficient than file_get_contents because file_get_contents is written entirely in C and skirts the issue of converting to a PHP string until the last minute. Of course the inefficiency might be entirely acceptable depending on the situation.ole wrote:Yeah I thought that too, which is why I retracted my last assertion but then there's something in the back of my brain telling me it's file_get_contents() or maybe that could be file_get_contents() versus fopen().file_get_contents() has the overhead of creating a PHP string as well as allocating memory to put that string in.
In any case my mantra is, code for clarity first, efficiency second.
You guys should really have a look at the manual once in a whileThe Manual wrote: file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Yes, astions, but I do not want to read the contents of a file into a string. I simply want to output it to the browser. And the manual page for readfile() says:
Seems like that's what I would want. Not sure what the output buffer is exactly, and how much of it I can use at any given time. =/Reads a file and writes it to the output buffer.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
I do not know enough about PHP's internals to say this definitively, but presumably the content will be streamed: PHP will not have any more in its memory at a time than it needs to; as it sends out data, it gets flushed from the memory and more stuff is read. In olden days, when memory was scarce, this concept was crucial to ensure that you didn't run out of memory (TeX is a big stickler about this thing... heck, it'll complain if the lines in your input files are too long).
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
You can do a benchmark.
Then do the same for file_get_contents() . If you can try the benchmark with small and large files and compare the performance of the 2 read functions.
Code: Select all
$start = time();
readfile('filename.jpeg');
$end = time();
echo "Operation took ".$end-$start." seconds";Unless you want to manipulate the output before you send it to the output buffer I would use readfile()
The output buffer is what is going to be sent to the end user.
echo, for example, sends information to the output buffer. As for working with the output buffer check out the output buffer PHP functions. http://us.php.net/manual/en/ref.outcontrol.php
The output buffer is what is going to be sent to the end user.
echo, for example, sends information to the output buffer. As for working with the output buffer check out the output buffer PHP functions. http://us.php.net/manual/en/ref.outcontrol.php
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Use file_get_contents(). It uses memory mapping and according to the docs is binary safe!
From Wikipedia:

From Wikipedia:
CheersMemory mapping is often faster than standard file based access for two reasons. The first is that the standard file I/O access routines require a system call (which is slower than a local function call). Memory mapping allows applications to ideally minimize the number of system calls for I/O. The other benefit is that most operating systems provide shared access to the underlying buffer cache (the kernel's cache of the file data), meaning that no copy be done to user space. Memory copying can be a time consuming portion of file I/O.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia