CodeIgniter Forums
How to download remote file without read it - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: How to download remote file without read it (/thread-31366.html)



How to download remote file without read it - El Forum - 06-16-2010

[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?


How to download remote file without read it - El Forum - 06-16-2010

[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');



How to download remote file without read it - El Forum - 06-16-2010

[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/1342583/php-manipulate-a-string-that-that-is-30-mil-chars-long