-
PHP 下载远程网页图片并且保存在本地实例
我们要取远程服务器中网页的图片然后保存到我们本地需要珍到php fopen或curl等等这类的函数,下面我给大家介绍几个常用的实例.
fopen函数实例
ob_start:打开输出缓冲
readfile:读入一个文件并写入到输出缓冲
返回从文件中读入的字节数,如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息.
ob_get_contents : Return the contents of the output buffer(返回输出缓冲的内容)
This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn’t active.(如果输出缓冲没有活动(打开),则返回 FALSE)
ob_end_clean() : Clean (erase) the output buffer and turn off output buffering(清除输出缓冲)
代码如下:
- <?php
- //URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字
- //默认把图片放在以此脚本相同的目录里
- function GrabImage($url,$filename=""){
- if($url == ""){
- return false;
- }
- $ext=strrchr($url,".");
- if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp" && $ext != ".png"){
- echo "格式不支持!";
- return false;
- }
- if($filename == ""){
- $filename = time()."$ext";
- }
- ob_start();
- readfile($url);
- $img=ob_get_contents();
- ob_end_clean();
- $size=strlen($img);
- $fp2=fopen($filename,"a");
- if(fwrite($fp2,$img) === false){
- echo "不能写入文件".$filename;
- exit();
- }else{
- echo "保存图片成功!";
- }
- fclose($fp2);
- return $filename;
- }
- //测试
- GrabImage("/logo.png","as.png");
- ?>
php下载远程图片函数,可伪造来路.
$gurl 要下载的图片地址
$rfurl 来路。如果目标图像做了防盗链设置,可以绕过。
$filename 下载图片保存的文件名,相对路径,不要用realpath
$gcookie 调整cookie 伪造的cookie
$JumpCount 跳转计数
$maxtime 最大次数
调用方法:
DownImageKeep(“http://www.baidu.com/img/baidu_jgylogo2.gif”,”http://baidu.com”,”a.gif”,”",0,10);
代码如下:
- function DownImageKeep($gurl, $rfurl, $filename, $gcookie=”", $JumpCount=0, $maxtime=30)
- {
- $urlinfos = GetHostInfo($gurl);
- $ghost = trim($urlinfos['host']);
- if($ghost==”)
- {
- return FALSE;
- }
- $gquery = $urlinfos['query'];
- if($gcookie==”" && !emptyempty($rfurl))
- {
- $gcookie = RefurlCookie($rfurl);
- }
- $sessionQuery = “GET $gquery HTTP/1.1rn”;
- $sessionQuery .= “Host: $ghostrn”;
- $sessionQuery .= “Referer: $rfurlrn”;
- $sessionQuery .= “Accept: */*rn”;
- $sessionQuery .= “User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn”;
- if($gcookie!=”" && !preg_match(“/[rn]/”, $gcookie))
- {
- $sessionQuery .= $gcookie.”rn”;
- }
- $sessionQuery .= “Connection: Keep-Alivernrn”;
- $errno = “”;
- $errstr = “”;
- $m_fp = fsockopen($ghost, 80, $errno, $errstr,10);
- fwrite($m_fp,$sessionQuery);
- $lnum = 0;
- //获取应答头
- $m_httphead = Array();
- $httpstas = explode(” “,fgets($m_fp,256));
- $m_httphead["http-edition"] = trim($httpstas[0]);
- $m_httphead["http-state"] = trim($httpstas[1]);
- while(!feof($m_fp))
- {
- $line = trim(fgets($m_fp,256));
- if($line == “” || $lnum>100)
- {
- break;
- }
- $hkey = “”;
- $hvalue = “”;
- $v = 0;
- for($i=0; $i {
- if($v==1)
- {
- $hvalue .= $line[$i];
- }
- if($line[$i]==”:”)
- {
- $v = 1;
- }
- if($v==0)
- {
- $hkey .= $line[$i];
- }
- }
- $hkey = trim($hkey);
- if($hkey!=”")
- {
- $m_httphead[strtolower($hkey)] = trim($hvalue);
- }
- }
- if(preg_match(“/^3/”, $m_httphead["http-state"]))
- {
- if(isset($m_httphead["location"]) && $JumpCount<3) { $JumpCount++; DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount); } else { return FALSE; } } if(!preg_match(“/^2/”, $m_httphead["http-state"])) { return FALSE; } if(!isset($m_httphead)) { return FALSE; } $contentLength = $m_httphead['content-length']; //保存图片 $fp = fopen($filename,”w”) or die(“写入文件:{$filename} 失败!”); $i=0; $okdata = “”; $starttime = time(); while(!feof($m_fp)) { $okdata .= fgetc($m_fp); $i++; //超时退出 if(time()-$starttime>$maxtime)
- {
- break;
- }
- //到达指定大小结束
- if($i >= $contentLength)
- {
- break;
- }
- }
- if($okdata!=”")
- {
- fwrite($fp,$okdata);
- }
- fclose($fp);
- if($okdata==”")
- {
- @unlink($filename);
- fclose($m_fp);
- return FALSE;
- }
- fclose($m_fp);
- return TRUE;
- }
- //获得网址的host和query部份
- function GetHostInfo($gurl)
- {
- $gurl = preg_replace(“/^http:///i”, “”, trim($gurl));
- $garr['host'] = preg_replace(“//(.*)$/i”, “”, $gurl);
- $garr['query'] = “/”.preg_replace(“/^([^/]*)//i”, “”, $gurl);
- return $garr;
- }
- //获得页面返回的Cookie信息
- function RefurlCookie($gurl)
- {
- global $gcookie,$lastRfurl;
- $gurl = trim($gurl);
- if(!empty($gcookie) && $lastRfurl==$gurl)
- {
- return $gcookie;
- }
- else
- {
- $lastRfurl=$gurl;
- }
- if(trim($gurl)==”)
- {
- return ”;
- }
- $urlinfos = GetHostInfo($gurl);
- $ghost = $urlinfos['host'];
- $gquery = $urlinfos['query'];
- $sessionQuery = “GET $gquery HTTP/1.1rn”;
- $sessionQuery .= “Host: $ghostrn”;
- $sessionQuery .= “Accept: */*rn”;
- $sessionQuery .= “User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn”;
- $sessionQuery .= “Connection: Closernrn”;
- $errno = “”;
- $errstr = “”;
- $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.’
- ‘);
- fwrite($m_fp,$sessionQuery);
- $lnum = 0;
- //获取详细应答头
- $gcookie = “”;
- while(!feof($m_fp))
- {
- $line = trim(fgets($m_fp,256));
- if($line == “” || $lnum>100)
- {
- break;
- }
- else
- {
- if(preg_match(“/^cookie/i”, $line))
- {
- $gcookie = $line;
- break;
- }
- }
- }
- fclose($m_fp);
- return $gcookie;
- }
出处:http://www.phpfensi.com/php/20140721/3832.html