Parse php download remote picture function can forge the antecedents

Source: Internet
Author: User
Tags php download save file trim

Gurl the picture address to download
$rfurl antecedents. If the target image does a burglar chain setting, you can bypass it.
$filename download picture saved file name, relative path, do not use Realpath
$gcookie to adjust cookie fake cookies
$JumpCount Jump Count
$maxtime maximum number of times
Call Method: Downimagekeep ("Baidu_jgylogo2.gif", "http://baidu.com", "A.gif", "", 0,10);

Copy Code code as follows:


<?php


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.1rn";


$sessionQuery. = "Host: $ghostrn";


$sessionQuery. = "Referer: $RFURLRN";


$sessionQuery. = "Accept: */*rn";


$sessionQuery. = "user-agent:mozilla/4.0 (compatible; MSIE 5.00; Windows) rn ";


if ($gcookie!= "" "&&!preg_match ("/[rn]/", $gcookie))


{


$sessionQuery. = $gcookie. " RN ";


}


$sessionQuery. = "Connection:keep-alivernrn";


$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 <strlen ($line); $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);


}


}


//Analysis 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 + +;


//Timeout End


if (Time ()-$starttime > $maxtime)


{


break;


}


//Reach 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;


}


/**


* Get 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.1rn";


$sessionQuery. = "Host: $ghostrn";


$sessionQuery. = "Accept: */*rn";


$sessionQuery. = "user-agent:mozilla/4.0 (compatible; MSIE 5.00; Windows) rn ";


$sessionQuery. = "Connection:closernrn";


$errno = "";


$errstr = "";


$m _fp = Fsockopen ($ghost, $errno, $errstr, ten) or Die ($ghost. ' <br/> ');


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;


}


/**


* Access to 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;


}


?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.