Gurl the image address to download
$rfurl route. If the target image does the anti-theft chain setting, it can be bypassed.
$filename download picture saved file name, relative path, do not use Realpath
$gcookie Adjust Cookie-Forged cookie
$JumpCount Jump Count
Maximum number of $maxtime
Calling Method: Downimagekeep ("Http://www.baidu.com/img/baidu_jgylogo2.gif", "http://baidu.com", "A.gif", "", 0,10);
Copy CodeThe code is as follows:
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 = = "" &&!empty ($rfurl))
{
$gcookie = Refurlcookie ($rfurl);
}
$sessionQuery = "GET $gquery http/1.1\r\n";
$sessionQuery. = "Host: $ghost \ r \ n";
$sessionQuery. = "Referer: $rfurl \ r \ n";
$sessionQuery. = "Accept: */*\r\n";
$sessionQuery. = "user-agent:mozilla/4.0 (compatible; MSIE 5.00; Windows 98) \ r \ n ";
if ($gcookie! = "" &&!preg_match ("/[\r\n]/", $gcookie))
{
$sessionQuery. = $gcookie. " \ r \ n ";
}
$sessionQuery. = "connection:keep-alive\r\n\r\n";
$errno = "";
$errstr = "";
$m _fp = Fsockopen ($ghost, $errno, $ERRSTR, 10);
Fwrite ($m _fp, $sessionQuery);
$lnum = 0;
Get detailed answer Header
$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);
}
}
Analyze Return Records
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 ');
Save File
$fp = fopen ($filename, "w") or Die ("Write file: {$filename} failed! ");
$i = 0;
$okdata = "";
$starttime = time ();
while (!feof ($m _fp))
{
$okdata. = fgetc ($m _fp);
$i + +;
End of timeout
if (Time ()-$starttime > $maxtime)
{
Break
}
reaches the specified size end
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;
}
/**
* Obtain the cookie information returned by a page
*
* @access Public
* @param string $gurl Adjust address
* @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, $errno, $errstr, ten) or Die ($ghost. '
');
Fwrite ($m _fp, $sessionQuery);
$lnum = 0;
Get detailed answer Header
$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;
}
/**
* Get the host and query part of the URL
*
* @access Public
* @param string $gurl Adjust address
* @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.html www.bkjia.com true http://www.bkjia.com/PHPjc/327912.html techarticle Gurl the image address to download $rfurl route. If the target image does the anti-theft chain setting, it can be bypassed. $filename Download Picture Save file name, relative path, do not use Realpath $ ...