php采集图片并显示,php采集内容中带有图片地址的远程图片并保存的方法

本文实例讲述了php采集内容中带有图片地址的远程图片并保存的方法。。具体实现方法如下:

代码如下: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;

}

代码如下:function get_remote($body,$title){

$img_array = array();

$img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址

//die($img_path);

$img_rpath='/upfile/news/'.date("y/m/d/");  //设置访问地址

$body = stripslashes(strtolower($body));

preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array);

$img_array = array_unique($img_array[2]);

foreach ($img_array as $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($img_path) )

{

$fp = fopen($img_path.$filename,"w");

if(fwrite($fp,$get_file)){

$body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);

}

fclose($fp);

@sleep(6);

}

}

}

 $body =str_replace("

return $body;

}

function mkdirs($dir)

{

if(!is_dir($dir)){

if(!mkdirs(dirname($dir))){

return false;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值