Downloading BIG files: fread() vs file_get_contents()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Downloading BIG files: fread() vs file_get_contents()

Post by jeff00seattle »

Hi

I am downloading BIG files: +20 MBytes

Both of the following code samples work.

My question is: At what point I should NOT consider using file_get_contents() sample and I should using fread() sample instead?

The PHP documentation states that fread() has a maximum read capability of 8192 bytes.

But the PHP documentation does not state for file_get_contents() what is its maximum read capability.

Using file_get_contents() to download BIG file

Code: Select all

$fpOut = fopen($strFilePath, "w+b"); 
 
$strContent = file_get_contents($strBlobURL);
if ( $strContent !== false ) {
    fwrite($fpOut, $strContent);
}
 
fclose($fpOut);
Using fread() to download BIG file

Code: Select all

$fpWrite = @fopen($strFilePath, "w+b");
$fpRead  = @fopen($strBlobURL, "rb");
 
while (!feof($fpRead)) {
    fwrite($fpWrite, fread($fpRead, 8192));
}
 
fclose($fpWrite);
fclose($fpRead);
Thanks

Jeff in Seattle
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Downloading BIG files: fread() vs file_get_contents()

Post by requinix »

Are you downloading a file from somewhere onto the server? Use copy instead.

file_get_contents gets the entire file as a string. For a 20MB file that means >20MB of memory in use. If you don't actually need to deal with the entire file at once then file_get_contents is a waste.
The note they give on how it's better than fread is for when people do something like

Code: Select all

$h = fopen($file, "rb");
$contents = fread($h, filesize($file));
fclose($h);
file_get_contents is much better for that purpose.

What you're trying to do is implement file copying. As I already said, use copy(), but if you were to do it yourself you'd want an fread loop - only dealing with a few KB of the file at a time. It's a bit slower but uses a fraction of the memory.


(And 20MB is not "big". That's more like "average". Come back when you're dealing with database dumps and site backups.)
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: Downloading BIG files: fread() vs file_get_contents()

Post by jeff00seattle »

Yes, copy() is good:

Code: Select all

if ( is_file($strTargetPath) ) {
    unlink($strTargetPath);
}
 
if ( !($fpWrite = fopen($strTargetPath, "w+b")) ) {
    throw new RuntimeException( "Failed create handle for target file \"{$strTargetPath}\"");
}
 
$bNetworkConnection = copy($strSourceURL, $strTargetPath);
 
fclose($fpWrite);
 
if ( !$bNetworkConnection 
    || !is_file($strTargetPath)
) {
    return false;
}
Thanks

Jeff in Seattle
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Downloading BIG files: fread() vs file_get_contents()

Post by requinix »

You don't need fopen/fclose to copy() a file. It's just like a regular file copy - not like you have to open file.txt in Notepad to move it from one folder to another.

In fact, depending on the OS, you might even have problems doing that. IIRC Unix is smart enough to deal but dumber OSes like Windows might not.
Post Reply