Page 1 of 1

Downloading BIG files: fread() vs file_get_contents()

Posted: Wed Mar 03, 2010 4:32 pm
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

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

Posted: Wed Mar 03, 2010 6:49 pm
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.)

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

Posted: Thu Mar 04, 2010 10:48 am
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

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

Posted: Thu Mar 04, 2010 5:36 pm
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.