hi,欢迎访问本站!
当前位置: 首页学习笔记正文

php下载远程图片到本地(总结)

用户投稿 学习笔记 16阅读
1. file_get_contents方法将图片下载到本地: <?phpfunction geturlpic($file_url, $save_to){ $content = file_get_contents($file_url); file_put_contents($save_to, $content);} 2. CURL简单方法将图片下载到本地: ​​​​​​​<?phpfunction geturlpic($file_url, $save_to){ $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$file_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $file_content = curl_exec($ch); curl_close($ch); $downloaded_file = fopen($save_to, 'w'); fwrite($downloaded_file, $file_content); fclose($downloaded_file);} 3. fopen方法将图片下载到本地: ​​​​​​​<?phpfunction geturlpic($file_url, $save_to){ $in= fopen($file_url, "rb"); //rb read as binary 二进制读取 $out= fopen($save_to, "wb"); //write as binary 二进制写入 while ($chunk = fread($in,8192)){ fwrite($out, $chunk, 8192); } fclose($in); fclose($out);} 4. 下载与保存完整方法(输出日志那行可删除):    <?phpprivate function downloadImage($url){         $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);        $file = curl_exec($ch);        curl_close($ch);        $this->saveAsImage($url, $file);    }private function saveAsImage($url, $file){        $filename = pathinfo($url, PATHINFO_BASENAME);        $dirname = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_DIRNAME);        $path =  'public' . $dirname . '/';        $fullpath =  'public' . $dirname . '/' . $filename;        // 如果目录不存在,则创建        if(!is_dir($path)) {            mkdir($path, 0777, true);        }        if(file_exists($fullpath)) {            //$this->output->writeln("【已存在】输出路径" . $fullpath);        } else {            $resource = fopen($fullpath, 'a');            fwrite($resource, $file);            fclose($resource);            //$this->output->writeln("【已保存】输出路径" . $fullpath);        }    } 5. CRUL方法将图片下载到本地: <?php$url = 'https://qiniu-cdn0.jinxidao.com/group1/M00/11/B5/ooYBAFp4CLOAVI5HAAUkC_QIsI4196.jpg';function http_get_data($url) { $ch = curl_init (); curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt ( $ch, CURLOPT_URL, $url ); ob_start (); curl_exec ( $ch ); $return_content = ob_get_contents (); ob_end_clean (); $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); return $return_content; } $return_content = http_get_data($url); $filename = 'test3.jpg'; $fp= @fopen($filename,"a"); //将文件绑定到流fwrite($fp,$return_content); //写入文件 ?> 6. 完美实现下载远程图片保存到本地 /* *功能:php完美实现下载远程图片保存到本地 *参数:文件url,保存文件目录,保存文件名称,使用的下载方式 *当保存文件名称为空时则使用远程文件原来的名称 */ function getImage($url,$save_dir='',$filename='',$type=0){ if(trim($url)==''){ return array('file_name'=>'','save_path'=>'','error'=>1); } if(trim($save_dir)==''){ $save_dir='./'; } if(trim($filename)==''){//保存文件名 $ext=strrchr($url,'.'); if($ext!='.gif'&&$ext!='.jpg'){ return array('file_name'=>'','save_path'=>'','error'=>3); } $filename=time().$ext; } if(0!==strrpos($save_dir,'/')){ $save_dir.='/'; } //创建保存目录 if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){ return array('file_name'=>'','save_path'=>'','error'=>5); } //获取远程文件所采用的方法 if($type){ $ch=curl_init(); $timeout=5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $img=curl_exec($ch); curl_close($ch); }else{ ob_start(); readfile($url); $img=ob_get_contents(); ob_end_clean(); } //$size=strlen($img); //文件大小 $fp2=@fopen($save_dir.$filename,'a'); fwrite($fp2,$img); fclose($fp2); unset($img,$url); return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0); } 7. 获取远程文件大小及信息的函数 <?phpfunction getFileSize($url){ $url = parse_url($url); if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){ fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n"); fputs($fp,"Host:$url[host]\r\n\r\n"); while(!feof($fp)){ $tmp = fgets($fp); if(trim($tmp) == ''){ break; }else if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){ return trim($arr[1]); } } return null; }else{ return null; } } echo getFileSize(http://www.dianpub.com/download/xml.rar)
标签:
声明:无特别说明,转载请标明本文来源!
发布评论
正文 取消