手机
当前位置:查字典教程网 >编程开发 >php教程 >php采集内容中带有图片地址的远程图片并保存的方法
php采集内容中带有图片地址的远程图片并保存的方法
摘要:本文实例讲述了php采集内容中带有图片地址的远程图片并保存的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:function...

本文实例讲述了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("<img","<img ",$body);

return $body;

}

function mkdirs($dir)

{

if(!is_dir($dir)){

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

return false;}

if(!mkdir($dir,0777)){

return false;}

}

return true;

}

//用法如下:

$str ='fasfsdafsa<img src=http://filesimg.xxxx.com/2010/03/2010062300391582.jpg />';

echo get_remote($str,'图片');

希望本文所述对大家的php程序设计有所帮助。

【php采集内容中带有图片地址的远程图片并保存的方法】相关文章:

php限制ip地址范围的方法

php通过exif_read_data函数获取图片的exif信息

PHP获取远程图片并保存到本地的方法

PHP ajax 异步执行不等待执行结果的处理方法

php将图片文件转换成二进制输出的方法

php获取网页里所有图片并存入数组的方法

php实现修改新闻时删除图片的方法

php生成随机密码的自定义函数

php实现根据IP地址获取其所在省市的方法

php生成圆角图片的方法

精品推荐
分类导航