This article introduces the content is about PHP download pictures of the method, has a certain reference value, now share to everyone, the need for friends can refer to
1.PHP provides a way to download images
download.php Code
<?php//gets the filename to download $filename = $_get[' filename '];//set header information header (' Content-disposition:attachment;filename= '. BaseName ($filename)); header (' Content-length: '. FileSize ($filename));//Read the file and write to the output buffer ReadFile ($filename);
HTML calls
<a href= "download.php?filename=./files/751c77c09d8e530520aab74234495f11.jpg" > Download images </a>
2.PHP 3 ways to download remote pictures
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_contentsfunction dlfile ($file _url, $save _to) {$content = File_ Get_contents ($file _url); File_put_contents ($save _to, $content);} 2. Use Curlfunction 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. Use Fopenfunction 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);}