Home >Backend Development >PHP Problem >How to save php network pictures to local

How to save php network pictures to local

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-26 15:53:564313browse

How to save php network pictures to local

PHP Get network pictures and save them in a local directory Ideas:

Related recommendations: "php Getting Started Tutorial"

Code As follows:

function file_exists_S3($url)
{
    $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容
    if($state){
        $filename = date("dMYHis").'.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;
    }
}

Call:

var_dump(file_exists_S3('http://images2015.cnblogs.com/blog/1156899/201705/1156899-20170518182731010-1445627236.png'));

The above is the detailed content of How to save php network pictures to local. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what are php array keysNext article:what are php array keys