file_get_contents vs readfile

file_get_contents vs readfile

am 10.09.2007 16:07:42 von howa

Target: To fetch a file using PHP and send to user

Method 1:

echo file_get_contents( $file_path );

Method 2:

readfile( $file_path );


Which one is better?

Re: file_get_contents vs readfile

am 10.09.2007 16:20:37 von zeldorblat

On Sep 10, 10:07 am, howa wrote:
> Target: To fetch a file using PHP and send to user
>
> Method 1:
>
> echo file_get_contents( $file_path );
>
> Method 2:
>
> readfile( $file_path );
>
> Which one is better?

In practice it probably doesn't make much of a difference. In this
case, however, I'd use readfile since the function was intended to do
exactly what it is that you're trying to do.

Re: file_get_contents vs readfile

am 10.09.2007 16:21:44 von Erwin Moller

howa wrote:
> Target: To fetch a file using PHP and send to user
>
> Method 1:
>
> echo file_get_contents( $file_path );
>
> Method 2:
>
> readfile( $file_path );
>
>
> Which one is better?
>

According to www.php.net:

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.

But that aside, I don't know what is better.
You could try to see if a huge file give better performance with
file_get_contents(), but I expect the networktraffic is a much bigger
concern than the servers file-IO.

Regards,
Erwin Moller

Re: file_get_contents vs readfile

am 10.09.2007 16:23:50 von howa

On 9 10 , 10 20 , ZeldorBlat wrote:
> On Sep 10, 10:07 am, howa wrote:
>
> > Target: To fetch a file using PHP and send to user
>
> > Method 1:
>
> > echo file_get_contents( $file_path );
>
> > Method 2:
>
> > readfile( $file_path );
>
> > Which one is better?
>
> In practice it probably doesn't make much of a difference. In this
> case, however, I'd use readfile since the function was intended to do
> exactly what it is that you're trying to do.

from the doc it said:

file_get_contents() is the preferred way to read the contents of a
file into a string

also, readfile() also need to read the entrie file into memory before
send to buffer, so I am just wondering which is better.

Re: file_get_contents vs readfile

am 10.09.2007 17:00:38 von gosha bine

On 10.09.2007 16:23 howa wrote:
> On 9 10 , 10 20 , ZeldorBlat wrote:
>> On Sep 10, 10:07 am, howa wrote:
>>
>>> Target: To fetch a file using PHP and send to user
>>> Method 1:
>>> echo file_get_contents( $file_path );
>>> Method 2:
>>> readfile( $file_path );
>>> Which one is better?
>> In practice it probably doesn't make much of a difference. In this
>> case, however, I'd use readfile since the function was intended to do
>> exactly what it is that you're trying to do.
>
> from the doc it said:
>
> file_get_contents() is the preferred way to read the contents of a
> file into a string
>
> also, readfile() also need to read the entrie file into memory before
> send to buffer, so I am just wondering which is better.
>
>

Actually, readfile doesn't read the whole file, it uses mmap if
possible, otherwise reads/writes in 8K chunks, thus conserving memory.



--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: file_get_contents vs readfile

am 10.09.2007 17:04:21 von Good Man

howa wrote in news:1189433262.872289.319540@
19g2000hsx.googlegroups.com:

> Target: To fetch a file using PHP and send to user
>
> Method 1:
>
> echo file_get_contents( $file_path );
>
> Method 2:
>
> readfile( $file_path );
>
>
> Which one is better?
>

I send files to users via PHP all the time, as my documents are stored
above the "www" directory. I use a modification of "readfile", found on
the manual page by chrisputnam at gmail dot com... had to use this
function because "readfile" has a knack for ending the file streaming
early on large files:


function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;

}

Re: file_get_contents vs readfile

am 10.09.2007 18:25:52 von howa

>> "readfile" has a knack for ending the file streaming
early on large files

not really understand your point....

i think beside memory limiation, a single call to readfile should be
more efficient.


On 9 10 , 11 04 , Good Man wrote:
> howa wrote in news:1189433262.872289.319540@
> 19g2000hsx.googlegroups.com:
>
> > Target: To fetch a file using PHP and send to user
>
> > Method 1:
>
> > echo file_get_contents( $file_path );
>
> > Method 2:
>
> > readfile( $file_path );
>
> > Which one is better?
>
> I send files to users via PHP all the time, as my documents are stored
> above the "www" directory. I use a modification of "readfile", found on
> the manual page by chrisputnam at gmail dot com... had to use this
> function because "readfile" has a knack for ending the file streaming
> early on large files:
>
> function readfile_chunked($filename,$retbytes=true) {
> $chunksize = 1*(1024*1024); // how many bytes per chunk
> $buffer = '';
> $cnt =0;
> // $handle = fopen($filename, 'rb');
> $handle = fopen($filename, 'rb');
> if ($handle === false) {
> return false;
> }
> while (!feof($handle)) {
> $buffer = fread($handle, $chunksize);
> echo $buffer;
> ob_flush();
> flush();
> if ($retbytes) {
> $cnt += strlen($buffer);
> }
> }
> $status = fclose($handle);
> if ($retbytes && $status) {
> return $cnt; // return num. bytes delivered like readfile() does.
> }
> return $status;
>
>
>
> }- -
>
> - -

Re: file_get_contents vs readfile

am 10.09.2007 18:36:21 von Good Man

howa wrote in news:1189441552.210767.228350
@r29g2000hsg.googlegroups.com:

>>> "readfile" has a knack for ending the file streaming
> early on large files
>
> not really understand your point....
>
> i think beside memory limiation, a single call to readfile should be
> more efficient.

try streaming a 12MB file, regardless of your memory settings, to
understand my point. your download will end early and you will not get the
complete file.

the function is posted on the manual page for a reason, i personally have
found it vital, use at your discretion.

Re: file_get_contents vs readfile

am 10.09.2007 19:18:11 von howa

okay. i get your point.

my files only will have Max 1MB or 2MB...

if file ending early regardless the memory limit, then this should a
bug...



On 9 11 , 12 36 , Good Man wrote:
> howa wrote in news:1189441552.210767.228350
> @r29g2000hsg.googlegroups.com:
>
> >>> "readfile" has a knack for ending the file streaming
> > early on large files
>
> > not really understand your point....
>
> > i think beside memory limiation, a single call to readfile should be
> > more efficient.
>
> try streaming a 12MB file, regardless of your memory settings, to
> understand my point. your download will end early and you will not get the
> complete file.
>
> the function is posted on the manual page for a reason, i personally have
> found it vital, use at your discretion.

Re: file_get_contents vs readfile

am 10.09.2007 19:27:22 von Michael Fesser

..oO(Good Man)

>try streaming a 12MB file, regardless of your memory settings, to
>understand my point. your download will end early and you will not get the
>complete file.

I did a test with a 179MB(!) file on my local server - no problems with
a plain readfile(). Memory limit is set to 32MB, PHP 5.2.3. Script
execution and file delivery took about 70s, memory usage was just 1.7MB.

>the function is posted on the manual page for a reason, i personally have
>found it vital, use at your discretion.

Maybe a bug in an older version? The UCN is from 2005.

Micha

Re: file_get_contents vs readfile

am 10.09.2007 19:41:36 von Good Man

Michael Fesser wrote in
news:e8vae355c9ihgr6tj87eg6qe36hlij7bb8@4ax.com:

> .oO(Good Man)
>
>>try streaming a 12MB file, regardless of your memory settings, to
>>understand my point. your download will end early and you will not
>>get the complete file.
>
> I did a test with a 179MB(!) file on my local server - no problems
> with a plain readfile(). Memory limit is set to 32MB, PHP 5.2.3.
> Script execution and file delivery took about 70s, memory usage was
> just 1.7MB.
>
>>the function is posted on the manual page for a reason, i personally
>>have found it vital, use at your discretion.
>
> Maybe a bug in an older version? The UCN is from 2005.

Interesting, thanks... as I said, it's not like I used that function right
away, I had to resort to it because my files were getting cut off.

I'll skip it on new coding projects and use plain ol' readfile()

:)

Re: file_get_contents vs readfile

am 10.09.2007 20:39:32 von Steve

"Good Man" wrote in message
news:Xns99A78B5C9DDE4sonicyouth@216.196.97.131...
> Michael Fesser wrote in
> news:e8vae355c9ihgr6tj87eg6qe36hlij7bb8@4ax.com:
>
>> .oO(Good Man)
>>
>>>try streaming a 12MB file, regardless of your memory settings, to
>>>understand my point. your download will end early and you will not
>>>get the complete file.
>>
>> I did a test with a 179MB(!) file on my local server - no problems
>> with a plain readfile(). Memory limit is set to 32MB, PHP 5.2.3.
>> Script execution and file delivery took about 70s, memory usage was
>> just 1.7MB.
>>
>>>the function is posted on the manual page for a reason, i personally
>>>have found it vital, use at your discretion.
>>
>> Maybe a bug in an older version? The UCN is from 2005.
>
> Interesting, thanks... as I said, it's not like I used that function right
> away, I had to resort to it because my files were getting cut off.
>
> I'll skip it on new coding projects and use plain ol' readfile()

or plain ol' file_get_contents()

;^)