例1
代码如下 复制代码
/*
*功能:php多种方式完美实现下载远程图片保存到本地
*参数:文件url,保存文件名称,使用的下载方式
*当保存文件名称为空时则使用远程文件原来的名称
*/
function getimage($url,$filename='',$type=0){
if($url==''){return false;}
if($filename==''){
$ext=strrchr($url,'.');
if($ext!='.gif' && $ext!='.jpg'){return false;}
$filename=time().$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;
}
例2
代码如下 复制代码
dedecms中的:
代码如下 复制代码
if(!empty($saveremoteimg))
{
$body = stripslashes($body);
$img_array = array();
preg_match_all(/(src|src)=[|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isu,$body,$img_array);
$img_array = array_unique($img_array[2]);
set_time_limit(0);
$imgurl = $img_dir./.strftime(%y%m%d,time());
$imgpath = $base_dir.$imgurl;
$millisecond = strftime(%h%m%s,time());
if(!is_dir($imgpath)) @mkdir($imgpath,0777);
foreach($img_array as $key =>$value)
{
$value = trim($value);
$get_file = @file_get_contents($value);
$rndfilename = $imgpath./.$millisecond.$key...substr($value,-3,3);
$fileurl = $imgurl./.$millisecond.$key...substr($value,-3,3);
if($get_file)
{
$fp = @fopen($rndfilename,w);
@fwrite($fp,$get_file);
@fclose($fp);
}
$body = ereg_replace($value,$fileurl,$body);
}
$body = addslashes($body);
}
?>
例4
代码如下 复制代码
http://www.bkjia.com/phpjc/444614.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444614.htmltecharticle在php中我们经常使用写一些简单的采集功能,这样可以自动把远程服务器的图片或资源直接采集保存到本地服务器中,下面我来给大家详细...
