CodeIgniter Forums

Full Version: How to download remote file without read it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]vlados2[/eluser]
Hi,
I want to download a remote file to the CI folder without reading it to a string.
I can use $string = read_file($file); and then put the string in a file, but can i do this without read in the memory? How I can optimize this?

El Forum

[eluser]SpooF[/eluser]
Well theres no way to really skip the memory part, but you can reduce the ammount of memory used. So instead of reading the enitre file into memory and then save it you can read a small portion at a time, saving it to disk along the way.

Code:
function save_file($inPath,$outPath)
{
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

save_file('http://www.someimagesite.com/img.jpg','image.jpg');

El Forum

[eluser]Burak Guzel[/eluser]
Look into cURL, and the CURLOPT_FILE option. I would assume it saves directly into the file instead of storing in memory.

Found a related question here:
http://stackoverflow.com/questions/13425...chars-long