php保存远程图片到本地

来源:百度知道 编辑:UC知道 时间:2024/06/16 11:08:07
function GrabImage($url,$filename="") {
if($url==""):return false;endif;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=date("dMYHis").$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;
}
$img=GrabImage("http://www.baidu.com/img/baidu_logo.gif",""); //地址
if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;

PHP的还有别的办法吗?最好越简单的越好..

<?php
$img = file_get_contents('http://www.baidu.com/img/baidu_logo.gif');
file_put_contents('1.gif',$img);
echo '<img src="1.gif">';
?>

$img = file_get_contents('http://www.91cici.com/images/logo.gif');
file_put_contents('test.gif',$img);

<?php
?>