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


Posted in PHP onJanuary 03, 2015

本文实例讲述了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 攻击方法之谈php+mysql注射语句构造
Oct 30 PHP
php URL验证正则表达式
Jul 19 PHP
解析PHP将对象转换成数组的方法(兼容多维数组类型)
Jun 21 PHP
解析centos中Apache、php、mysql 默认安装路径
Jun 25 PHP
php防止sql注入代码实例
Dec 18 PHP
PHP中strtr字符串替换用法详解
Nov 26 PHP
php生成唯一的订单函数分享
Feb 02 PHP
ucenter中词语过滤原理分析
Jul 13 PHP
php判断是否连接上网络的方法实例详解
Dec 14 PHP
PHP实现字符串的全排列详解
Apr 24 PHP
laravel 关联关系遍历数组的例子
Oct 10 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
Feb 27 PHP
php中socket通信机制实例详解
Jan 03 #PHP
php读取csv数据保存到数组的方法
Jan 03 #PHP
php获取用户浏览器版本的方法
Jan 03 #PHP
php堆排序实现原理与应用方法
Jan 03 #PHP
php购物车实现方法
Jan 03 #PHP
PHP实现格式化文件数据大小显示的方法
Jan 03 #PHP
php自定义加密与解密程序实例
Dec 31 #PHP
You might like
PHP制作图型计数器的例子
2006/10/09 PHP
PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径
2014/06/30 PHP
mac环境中使用brew安装php5.5.15
2014/08/18 PHP
php面向对象值单例模式
2016/05/03 PHP
Yii框架结合sphinx,Ajax实现搜索分页功能示例
2016/10/18 PHP
利用javascript/jquery对上传文件格式过滤的方法
2009/07/25 Javascript
js 无提示关闭浏览器页面的代码
2010/03/09 Javascript
js 图片随机不定向浮动的实现代码
2013/07/02 Javascript
javascript获取隐藏dom的宽高 具体实现
2013/07/14 Javascript
js计算两个时间之间天数差的实例代码
2013/11/19 Javascript
让IE8浏览器支持function.bind()方法
2014/10/16 Javascript
JS实现页面超时后自动跳转到登陆页面
2015/01/19 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
基于vue2.x的电商图片放大镜插件的使用
2018/01/22 Javascript
js数组去重的N种方法(小结)
2018/06/07 Javascript
Bootstrap table表格初始化表格数据的方法
2018/07/25 Javascript
Vue自定义弹窗指令的实现代码
2018/08/13 Javascript
jQuery/JS监听input输入框值变化实例
2019/10/17 jQuery
webpack.DefinePlugin与cross-env区别详解
2020/02/23 Javascript
python回调函数的使用方法
2014/01/23 Python
python采用getopt解析命令行输入参数实例
2014/09/30 Python
Python使用sorted对字典的key或value排序
2018/11/15 Python
Python中shapefile转换geojson的示例
2019/01/03 Python
对Python中DataFrame选择某列值为XX的行实例详解
2019/01/29 Python
用Cython加速Python到“起飞”(推荐)
2019/08/01 Python
Python检查 云备份进程是否正常运行代码实例
2019/08/22 Python
Python3常见函数range()用法详解
2019/12/30 Python
在tensorflow实现直接读取网络的参数(weight and bias)的值
2020/06/24 Python
施华洛世奇意大利官网:SWAROVSKI意大利
2018/07/23 全球购物
Armor Lux法国官方网站:水手服装、成衣和内衣
2020/05/26 全球购物
存储过程和函数的区别
2013/05/28 面试题
求职简历自荐信范文
2013/10/21 职场文书
婚礼答谢宴主持词
2014/03/14 职场文书
责任书格式范文
2014/07/28 职场文书
html输入两个数实现加减乘除功能
2021/07/01 HTML / CSS
Moment的feature导致线上bug解决分析
2022/09/23 Javascript