473,387 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

file_get_contents vs readfile

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?

Sep 10 '07 #1
9 20958
On Sep 10, 10:07 am, howa <howac...@gmail.comwrote:
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.

Sep 10 '07 #2
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
Sep 10 '07 #3
On 9 10 , 10 20 , ZeldorBlat <zeldorb...@gmail.comwrote:
On Sep 10, 10:07 am, howa <howac...@gmail.comwrote:
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.
Sep 10 '07 #4
On 10.09.2007 16:23 howa wrote:
On 9 10 , 10 20 , ZeldorBlat <zeldorb...@gmail.comwrote:
>On Sep 10, 10:07 am, howa <howac...@gmail.comwrote:
>>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
Sep 10 '07 #5
howa <ho******@gmail.comwrote 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.
Sep 10 '07 #6
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 <he...@letsgo.comwrote:
howa <howac...@gmail.comwrote 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.

Sep 10 '07 #7
..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
Sep 10 '07 #8
Michael Fesser <ne*****@gmx.dewrote in
news:e8********************************@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()

:)

Sep 10 '07 #9
"Good Man" <he***@letsgo.comwrote in message
news:Xn************************@216.196.97.131...
Michael Fesser <ne*****@gmx.dewrote in
news:e8********************************@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()

;^)
Sep 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Yannick Bétemps | last post by:
Hi all, As says the topic, I recently encountered problems with customers using one of my file management applications and being protected in the same time with Norton Internet Security firewall....
12
by: ORC | last post by:
Shouldn't 'ReadFile' block when timeouts are specified even when running in overlapped mode or am I wrong ??? Thanks Ole
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
1
by: jjainschigg | last post by:
I'm running PHP 5 on a shared VPS. I have a couple little apps that are using file_get_contents() and readfile() for various kinds of (what should be) low-overhead, fast outbound accesses ... that...
5
by: howa | last post by:
are there any advantage in replacing all fread() operations with file_get_contents() ? i.e. file_get_contents("/usr/local/something.txt") VS $filename = "/usr/local/something.txt";
4
by: Eric Renken | last post by:
I am trying to do an Overlapped ReadFile on a HID device and it just isn't working for me. The WaitForSingleObject keeps giving me an error "The system cannot find the file specified." This...
6
by: Taras_96 | last post by:
Hi everyone, The output of echo file_get_contents("http://watchout4snakes.com/creativitytools/ RandomWord/RandomWordPlus.aspx"); leaves the browser empty.. no error messages, nothing. ...
0
by: pac1250 | last post by:
Hi, I am searching how to solve a problem and I dont find it :( I want to access a page from a script behind a proxy : (my script) <-(a proxy with authentification) <-(https serveur with...
15
by: Ketchup | last post by:
Hello everyone, I have been stuck with this problem for quite some time now. I am working in VB.NET, using framework 1.0. I have to keep the compatibility down to the original .NET framework...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.