Home >Backend Development >PHP Tutorial >Local connection is restricted or no connection. How to save remote images locally under PHP

Local connection is restricted or no connection. How to save remote images locally under PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:43:32958browse

Today, when I was sorting out information, I found a function that I had found before. Through this function, we can achieve the above function.
Main function:

Copy code The code is as follows:


function GrabImage($url,$filename="") {
if($url=="") return false;
if($filename= ="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
$filename=date("YmdHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($ img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}


Get a picture Code:

Copy code The code is as follows:


$img=GrabImage("http://www.baidu.com/img/baidu_logo.gif","logo.gif");
if($ img){
echo '';
}else{
echo "false";
}


This is an example of saving Google's logo, obtained Pictures are saved in the same directory.
Get a series of regular pictures (for example: 100 pictures named with numbers 1-100):

Copy the code The code is as follows:


for ($i=1;$i<=100 ;$i++){
$img=GrabImage("http://www.yourimagesite.com/images/$i.gif","images/$i.gif");
}


www.yourimagesite above .com is the URL of the image and needs to be modified by yourself. After the program is executed, all images will be saved to the images directory.

The above introduces the method of saving remote pictures to the local under PHP with limited or no local connection, including the content of limited or no local connection. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn