8-19 3,110 views
使用php的curl方法下载图片,只需要提供图片地址就可以了。
$url = "http://www.bac.com/1.gif";
getImage($url,'./');
function getImage($url,$path='',$filename='',$type=0){
if($url==''){return false;}
if($filename==''){
$ext=pathinfo($url, PATHINFO_EXTENSION);
if(!empty($ext)){
$ext = '.'.$ext;
}
if ($path!=''){
$path = $path . date('Y/').date('m/');
if (!is_dir($path)){
@mkdir($path,777,true);
}
}
$filename=$path . date('YmdHis').'_curl_'.time().rand(100,999).$ext;
}
//文件保存路径
if($type){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$img=curl_exec($ch);
curl_close($ch);
}else{
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;
}