php使用cur自动采集下载远程服务器图片方法
听他们说php里面的cur函数也可以下载保存外部图片,有人有代码分享下吗
听他们说php里面的cur函数也可以下载保存外部图片,有人有代码分享下吗
答案:<?php
$url = "http://www.zhaoxi.net/images/mymake/2012/9/2012092605520443998.gif";
$filename = 'zhaoxi.gif';getImg($url, $filename);
function getImg($url = "", $filename = "") {
if(is_dir(basename($filename))) {
echo "The Dir was not exits";
Return false;
}
//去除URL连接上面可能的引号
$url = preg_replace( '/(?:^['"]+|['"/]+$)/', '', $url );
$hander = curl_init();
$fp =fopen($filename,'wb');
curl_setopt($hander,CURLOPT_URL,$url);
curl_setopt($hander,CURLOPT_FILE,$fp);
curl_setopt($hander,CURLOPT_HEADER,0);
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
//curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
curl_setopt($hander,CURLOPT_TIMEOUT,60);
curl_exec($hander);
curl_close($hander);
fclose($fp);
Return true;
}
?>