Welcome Guest, Not a member yet? Register   Sign In
How to download remote file without read it
#1

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

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB