Home Backend Development PHP Tutorial PHP download pictures remotely to local

PHP download pictures remotely to local

Jul 29, 2016 am 09:05 AM
dir gt save url

 Hello everyone, starting from today, I will start writing a blog. I will record the solutions to the problems I encountered at work, so that I can find them later and provide convenience to others. I am not talented. It is my first time to write a blog. What can I do? Please point out any shortcomings, thank you!

Today’s record is about how to remotely download images to a local computer through PHP. Without further ado, let’s get into the code.

&lt;?php<br />/*<br />*功能:php完美实现下载远程图片保存到本地<br />*参数:文件url,保存文件目录,保存文件名称,使用的下载方式<br />*当保存文件名称为空时则使用远程文件原来的名称<br />*/<br />function getImage($url,$save_dir='',$filename='',$type=0){<br />    if(trim($url)==''){<br />        return array('file_name'=&gt;'','save_path'=&gt;'','error'=&gt;1);<br>    }<br>    if(trim($save_dir)==''){<br>        $save_dir='./';<br>    }<br>    if(trim($filename)==''){//保存文件名<br>        $ext=strrchr($url,'.');<br>        if($ext!='.gif'&amp;&amp;$ext!='.jpg'){<br>            return array('file_name'=&gt;'','save_path'=&gt;'','error'=&gt;3);<br>        }<br>        $filename=time().$ext;<br>    }<br>    if(0!==strrpos($save_dir,'/')){<br>        $save_dir.='/';<br>    }<br>    //创建保存目录<br>    if(!file_exists($save_dir)&amp;&amp;!mkdir($save_dir,0777,true)){<br>        return array('file_name'=&gt;'','save_path'=&gt;'','error'=&gt;5);<br>    }<br>    //获取远程文件所采用的方法<br>    if($type){<br>        $ch=curl_init();<br>        $timeout=5;<br>        curl_setopt($ch,CURLOPT_URL,$url);<br>        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);<br>        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);<br>        $img=curl_exec($ch);<br>        curl_close($ch);<br>    }else{<br>        ob_start();<br>        readfile($url);<br>        $img=ob_get_contents();<br>        ob_end_clean();<br>    }<br>    //$size=strlen($img);<br>    //文件大小<br>    $fp2=@fopen($save_dir.$filename,'a');<br>    fwrite($fp2,$img);<br>    fclose($fp2);<br>    unset($img,$url);<br>    return array('file_name'=&gt;$filename,'save_path'=&gt;$save_dir.$filename,'error'=&gt;0);<br>}<br>?&gt;<br>然后直接调用函数就可以把图片下载到本地
Copy after login
rrree

The above introduces the remote downloading of pictures to the local in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

PHP function introduction—get_headers(): Get the response header information of the URL PHP function introduction—get_headers(): Get the response header information of the URL Jul 25, 2023 am 09:05 AM

PHP function introduction—get_headers(): Get the response header information of the URL

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

Why NameResolutionError(self.host, self, e) from e and how to solve it

How to get your Steam ID in a few steps? How to get your Steam ID in a few steps? May 08, 2023 pm 11:43 PM

How to get your Steam ID in a few steps?

How to use URL encoding and decoding in Java How to use URL encoding and decoding in Java May 08, 2023 pm 05:46 PM

How to use URL encoding and decoding in Java

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

What is the difference between html and url What is the difference between html and url Mar 06, 2024 pm 03:06 PM

What is the difference between html and url

Scrapy optimization tips: How to reduce crawling of duplicate URLs and improve efficiency Scrapy optimization tips: How to reduce crawling of duplicate URLs and improve efficiency Jun 22, 2023 pm 01:57 PM

Scrapy optimization tips: How to reduce crawling of duplicate URLs and improve efficiency

See all articles