Home >Backend Development >PHP Tutorial >How to save network images in php (code)

How to save network images in php (code)

不言
不言Original
2018-08-20 17:09:168446browse

The content of this article is about how to save network images (code) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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;
           }
    }

Note: $filename can be rewritten to the path you need

Related recommendations:

Code implementation of vertical merging & horizontal merging of two-dimensional arrays in php

Solution to float to int distortion in PHP

The above is the detailed content of How to save network images in php (code). 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