This article mainly introduces the PHP download remote pictures of several ways to summarize the relevant information, the need for friends can refer to the following
Summary of several methods for downloading remote images in PHP
This article shows 3 ways to download pictures from a remote URL and save them to a local file, 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);}
The above is the whole content of this article, I hope that everyone's study has helped.