php保存网络图片到本地

//保存网络图片
    function getimg($url)

{    

$state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容

    if($state){        

    $filename = './caiji/'.time().'.jpg';//文件名称与路径

        ob_start();//打开输出

        readfile($url);//输出图片文件

        $img = ob_get_contents();//得到浏览器输出

        ob_end_clean();//清除输出并关闭

        $size = strlen($img);//得到图片大小

        $fp2 = @fopen($filename, "a");        

        fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名

        fclose($fp2);        

        return 1;

    } else{        

           return 0;

           }

    }

猜你喜欢

转载自www.cnblogs.com/jcydd/p/10902853.html