php采集下载远程图片

    <?php  
    set_time_limit(0);//设置PHP超时时间
    $url = "http://www.xiuren8.com/thread-832.htm";  
    $content=file_get_contents($url);  
    $reg="/<img.*?src=\"(.*?)\".*?>/";  
      
    preg_match_all($reg,$content,$matches);  
      
    $path = './imgDownload';  
    if(!file_exists($path)){  
        mkdir($path, 0777);  
    }  
      
    for($i = 0;$i < count($matches[1]);$i ++){  
          
        /*explode 
        $url_arr[$i] = explode('/', $matches[1][$i]); 
        $last = count($url_arr[$i])-1; 
        */  
          
        //strrchr     
        $filename = strrchr($matches[1][$i], '/');  
          
        downImage($matches[1][$i],$path.$filename);  
        //downImage($matches[1][$i],$path.'/'.$url_arr[$i][$last]);  
    }  
      
    function downImage($url,$filename="") {  
        if($url=="") return false;  
      
        if($filename=="") {  
            $ext=strrchr($url,".");  
            if($ext!=".gif" && $ext!=".jpg" && $ext!=".png" && $ext!="jpeg") return false;  
            $filename=date("YmdHis").$ext;  
        }  
      
        ob_start();  
        //make file that output from url goes to buffer  
        readfile($url);  
        //file_get_contents($url);  这个方法不行的!!!只能用readfile  
        $img = ob_get_contents();  
        ob_end_clean();  
      
        $fp=@fopen($filename, "a");//append  
        fwrite($fp,$img);  
          
        fclose($fp);  
      
        return $filename;  
    }

  

posted @ 2017-08-03 11:20  NBTiger  阅读(224)  评论(0编辑  收藏  举报