|
本文实例讲述了php采集内容中带有图片地址的远程图片并保存的方法。分享给大家供大家参考。具体实现方法如下:
[U]复制代码[/U] 代码如下:function my_file_get_contents($url, $timeout=30) {
if ( function_exists('curl_init') )
{
$ch = curl_init();
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )
{
$file_contents = @file_get_contents($url);
}
else
{
$file_contents = '';
}
return $file_contents;
}
[U]复制代码[/U] 代码如下:function get_remote($body,$title){
$();
$("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址
//die($);
$'/upfile/news/'.date("y/m/d/"); //设置访问地址
$body = stripslashes(strtolower($body));
preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$);
$($[2]);
foreach ($$key => $value) {
$get_file = my_file_get_contents($value,60);
$filetime = time();
$filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);
if(emptyempty($get_file)){
@sleep(10);
$get_file = my_file_get_contents($value,30);
if(emptyempty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
continue;
}
}
if(!emptyempty($get_file) ){
if( mkdirs($) )
{
$fp = fopen($$filename,"w");
if(fwrite($fp,$get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", $$filename, $body);
}
fclose($fp);
@sleep(6);
}
}
}
$body =str_replace("
[img][/img]
';
echo get_remote($str,'图片');
希望本文所述对大家的php程序设计有所帮助。
您可能感兴趣的文章:使用ThinkPHP自带的Http类下载远程图片到本地的实现代码php中通过正则表达式下载内容中的远程图片的函数代码解析php下载远程图片函数 可伪造来路一个PHP的远程图片抓取函数分享PHP实现的下载远程图片自定义函数分享php中使用gd库实现远程图片下载实例PHP获取远程图片并保存到本地的方法php将远程图片保存到本地服务器的实现代码php正则匹配文章中的远程图片地址并下载图片至本地
|
|