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);
复制代码 代码如下:
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 {
$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;
}
/**
* 获得某页面返回的cookie信息
*
* @access public
* @param string $gurl 调整地址
* @return string
*/
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.1\r\n;
$sessionquery .= host: $ghost\r\n;
$sessionquery .= accept: */*\r\n;
$sessionquery .= user-agent: mozilla/4.0 (compatible; msie 5.00; windows 98)\r\n;
$sessionquery .= connection: close\r\n\r\n;
$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;
}
/**
* 获得网址的host和query部份
*
* @access public
* @param string $gurl 调整地址
* @return string
*/
function gethostinfo($gurl)
{
$gurl = preg_replace(/^http:\/\//i, , trim($gurl));
$garr['host'] = preg_replace(/\/(.*)$/i, , $gurl);
$garr['query'] = /.preg_replace(/^([^\/]*)\//i, , $gurl);
return $garr;
}
?>
http://www.bkjia.com/phpjc/327912.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/327912.htmltecharticlegurl 要下载的图片地址 $rfurl 来路。如果目标图像做了防盗链设置,可以绕过。 $filename 下载图片保存的文件名,相对路径,不要用realpath $...