Home >Backend Development >PHP Tutorial >Local connection is restricted or no connection. How to save remote images locally under PHP
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;
}
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";
}
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");
}
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.