Nathan Sri
Nathan Sri

Reputation: 103

downloading facebook image to local server

i am using

function save_image($inPath,$outPath){
    //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

This function working perfectly , but if i open the image it show file is empty.

i also tried file_get_content() function .But still having the same problem.

Upvotes: 1

Views: 1846

Answers (1)

ChrisR
ChrisR

Reputation: 14447

I always prefer to use curl to do this kind of thing.

function saveImage($url, $savePath) 
{

    $ch = curl_init($url);
    $fp = fopen($savePath, 'wb');

    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $result = curl_exec($ch);

    fclose($fp);
    curl_close($ch);

    return $result;

}

If you don't have curl installed check the official PHP curl installation instructions, installing curl of Windows is pretty easy.

Upvotes: 1

Related Questions