• 技术文章 >后端开发 >php教程

    php自动采集内容中带有图片地址的远程图片保存到本地_PHP教程

    2016-07-20 11:08:03原创394
    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/").'//m.sbmmt.com/m/'.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(empty($get_file)){
    @sleep(10);
    $get_file = my_file_get_contents($value,30);
    if(empty($get_file)){
    $body = preg_replace("//m.sbmmt.com/m/".addcslashes($value,"//m.sbmmt.com/m/")."/isu", '/notfound.jpg', $body);
    continue;
    }
    }
    if(!empty($get_file) ){
    if( mkdirs($img_path) )
    {
    $fp = fopen($img_path.$filename,"w");
    if(fwrite($fp,$get_file)){
    $body = preg_replace("//m.sbmmt.com/m/".addcslashes($value,"//m.sbmmt.com/m/")."/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;}
    if(!mkdir($dir,0777)){
    return false;}
    }
    return true;
    }

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

    本站原创教程转请注明来源,文件上传代码会这样做。


    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444916.htmlTechArticlephp自动采集内容中带有图片地址的远程图片保存到本地 function my_file_get_contents($url, $timeout=30){ if ( function_exists('curl_init') ) { $ch = curl_init();...

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php 自动 采集 内容 中带 有图片 地址 远程 图片 保存 本地 php 自动 采集 内容 中带 有图片
    上一篇:php堆排序实现原理与应用程序代码_PHP教程 下一篇:php获取flv视频时间长度代码_PHP教程
    PHP编程就业班

    相关文章推荐

    • 我28了,还能不能学PHP?• 弄php的iter过来报到• php程序重要是正则协助 很急很• 文件上传,该怎么处理• php 数据能够获取 但没法写入数据库 同一文件另一个表可能写入

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网