Home > Backend Development > PHP Tutorial > PHP download pictures remotely to local, _PHP tutorial

PHP download pictures remotely to local, _PHP tutorial

WBOY
Release: 2016-07-12 09:00:22
Original
969 people have browsed it

php remotely downloads pictures to local,

Hello everyone, starting from today, I started to write a blog and record the solutions to the problems I encountered at work. It will be convenient for future searches, and it will also be convenient for others. I am not very talented. It is my first time to write a blog. Please point out any shortcomings. Thank you!

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

<?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'=>'','save_path'=>'','error'=>1);<br />    }<br />    if(trim($save_dir)==''){<br />        $save_dir='./';<br />    }<br />    if(trim($filename)==''){//保存文件名<br />        $ext=strrchr($url,'.');<br />        if($ext!='.gif'&&$ext!='.jpg'){<br />            return array('file_name'=>'','save_path'=>'','error'=>3);<br />        }<br />        $filename=time().$ext;<br />    }<br />    if(0!==strrpos($save_dir,'/')){<br />        $save_dir.='/';<br />    }<br />    //创建保存目录<br />    if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){<br />        return array('file_name'=>'','save_path'=>'','error'=>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'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);<br />}<br />?><br />然后直接调用函数就可以把图片下载到本地
Copy after login
$img = getImage("$url","images/");
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1094276.htmlTechArticlephp Remote download pictures to local, Hello everyone, starting from today, I started to write a blog and put myself at work Record the solutions to the problems you encountered so that you can find them later, also...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template