PHP 爬取图片 保存本地

    public function getImage($url,$filename='')
    {
        if($url == ''){
            return false;
        }
        if($filename == ''){
            $ext = pathinfo($url);
            if($ext['extension'] != "jpg" && $ext['extension'] != "png"){
                return false;
            }
            $filename = time() .".". $ext['extension'];
        }
        //文件保存路径
        ob_start();
        readfile ($url);
        $img = ob_get_contents();
        ob_end_clean();
        $size = strlen($img);
        $fp2 = fopen($filename,"a");
        fwrite($fp2,$img);
        fclose($fp2);
        return $filename;
    }
 getImage("http://xxxxx.jpg");

posted @ 2018-06-07 14:12  宁佳兵  阅读(496)  评论(0编辑  收藏  举报