This article demonstrates 3 download pictures from a remote URL. and saved to the local file method, including File_get_contents,curl and fopen.
1. Using file_get_contents
function Dlfile ($file _url, $save _to)
{
$content = file_get_contents ($file _url);
File_put_contents ($save _to, $content);
}
2. Using Curl
function Dlfile ($file _url, $save _to)
{
$ch = Curl_init ();
curl_setopt ($ch, curlopt_post, 0);
curl_setopt ($ch, Curlopt_url, $file _url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
$file _content = curl_exec ($ch);
Curl_close ($ch);
$downloaded _file = fopen ($save _to, ' W ');
Fwrite ($downloaded _file, $file _content);
Fclose ($downloaded _file);
}
3. Using fopen
function Dlfile ($file _url, $save _to)
{
$in = fopen ($file _url, "RB");
$out = fopen ($save _to, "WB");
while ($chunk = Fread ($in, 8192))
{
Fwrite ($out, $chunk, 8192);
}
Fclose ($in);
Fclose ($out);
}
by Iefreer
PHP 3 ways to download remote images