473,833 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file_get_conten ts vs readfile

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

Method 1:

echo file_get_conten ts( $file_path );

Method 2:

readfile( $file_path );
Which one is better?

Sep 10 '07 #1
9 21013
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_conten ts( $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_conten ts( $file_path );

Method 2:

readfile( $file_path );
Which one is better?
According to www.php.net:

file_get_conten ts() 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_conten ts(), 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...@gma il.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_conten ts( $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_conten ts() 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...@gma il.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_conten ts( $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_conten ts() 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.go oglegroups.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.c omwrote:
howa <howac...@gmail .comwrote in news:1189441552 .210767.228350
@r29g2000hsg.go oglegroups.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.de wrote in
news:e8******** *************** *********@4ax.c om:
.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.c omwrote in message
news:Xn******** *************** *@216.196.97.13 1...
Michael Fesser <ne*****@gmx.de wrote in
news:e8******** *************** *********@4ax.c om:
>.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_conten ts()

;^)
Sep 10 '07 #10

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

Similar topics

0
1319
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. When the Security is not activated, downloading files works fine, but as soon as it is activated they can not download anything bigger than 500kb ! I checked in the source code, and whatever the method to read the file on the server (readfile,...
12
6907
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
5737
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 following is a diskette class (vb .net) that works find, in that I can validate a diskette is mounted, dismount it, lock it, unlock it, get diskette geometry, etc., all with a valid handle from CreateFile API! I can even position the file pointer,...
1
5049
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 is, I'm handing URLencoded arguments to a couple fast servers and grabbing small patches of XML in return. When I enter these URL+argument strings in a browser (with the caches turned off), I get effectively instant responses -- so the remote...
5
23040
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
14008
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 code need to work in 64bit and 32bit, so I am using IntPtr for pointers instead of int. Here is the structure of OVERLAPPED public struct OVERLAPPED
6
6954
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. Why is this occurring?
0
5020
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 authentification) -----------------------------------------------------------------
15
5948
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 as much as possible. I am forced to use API calls for several functions, one of which is ReadFile. The reason is because the framework seems to use ANSI versions of all API functions, limiting the MAX_PATH for filenames to about 260 chars for ...
0
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10543
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10213
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4422
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.