PHP download get remote picture function (can forge route)

Source: Internet
Author: User
Tags php download
  1. Download get remote Pictures
  2. function Downimagekeep ($gurl, $rfurl, $filename, $gcookie = "", $JumpCount =0, $maxtime =30)
  3. {
  4. $urlinfos = GetHostInfo ($gurl);
  5. $ghost = Trim ($urlinfos [' Host ']);
  6. if ($ghost = = ")
  7. {
  8. return FALSE;
  9. }
  10. $gquery = $urlinfos [' query '];
  11. if ($gcookie = = "" &&!empty ($rfurl))
  12. {
  13. $gcookie = Refurlcookie ($rfurl);
  14. }
  15. $sessionQuery = "GET $gquery http/1.1\r\n";
  16. $sessionQuery. = "Host: $ghost \ r \ n";
  17. $sessionQuery. = "Referer: $rfurl \ r \ n";
  18. $sessionQuery. = "Accept: */*\r\n";
  19. $sessionQuery. = "user-agent:mozilla/4.0 (compatible; MSIE 5.00; Windows 98) \ r \ n ";
  20. if ($gcookie! = "" &&!preg_match ("/[\r\n]/", $gcookie))
  21. {
  22. $sessionQuery. = $gcookie. " \ r \ n ";
  23. }
  24. $sessionQuery. = "connection:keep-alive\r\n\r\n";
  25. $errno = "";
  26. $errstr = "";
  27. $m _fp = Fsockopen ($ghost, $errno, $ERRSTR, 10);
  28. Fwrite ($m _fp, $sessionQuery);
  29. $lnum = 0;
  30. Get detailed answer Header
  31. $m _httphead = Array ();
  32. $httpstas = Explode ("", Fgets ($m _fp,256));
  33. $m _httphead["http-edition") = Trim ($httpstas [0]);
  34. $m _httphead["http-state") = Trim ($httpstas [1]);
  35. while (!feof ($m _fp))
  36. {
  37. $line = Trim (fgets ($m _fp,256));
  38. if ($line = = "" | | $lnum >100)
  39. {
  40. Break
  41. }
  42. $hkey = "";
  43. $hvalue = "";
  44. $v = 0;
  45. for ($i =0; $i
  46. {
  47. if ($v ==1)
  48. {
  49. $hvalue. = $line [$i];
  50. }
  51. if ($line [$i]== ":")
  52. {
  53. $v = 1;
  54. }
  55. if ($v ==0)
  56. {
  57. $hkey. = $line [$i];
  58. }
  59. }
  60. $hkey = Trim ($hkey);
  61. if ($hkey! = "")
  62. {
  63. $m _httphead[strtolower ($hkey)] = Trim ($hvalue);
  64. }
  65. }
  66. Analyze Return Records
  67. if (Preg_match ("/^3/", $m _httphead["Http-state"]))
  68. {
  69. if (Isset ($m _httphead["location") && $JumpCount <3)
  70. {
  71. $JumpCount + +;
  72. Downimagekeep ($gurl, $rfurl, $filename, $gcookie, $JumpCount);
  73. }
  74. Else
  75. {
  76. return FALSE;
  77. }
  78. }
  79. if (!preg_match ("/^2/", $m _httphead["Http-state"]))
  80. {
  81. return FALSE;
  82. }
  83. if (!isset ($m _httphead))
  84. {
  85. return FALSE;
  86. }
  87. $contentLength = $m _httphead[' content-length ');
  88. Save File
  89. $fp = fopen ($filename, "w") or Die ("Write file: {$filename} failed! ");
  90. $i = 0;
  91. $okdata = "";
  92. $starttime = time ();
  93. while (!feof ($m _fp))
  94. {
  95. $okdata. = fgetc ($m _fp);
  96. $i + +;
  97. End of timeout
  98. if (Time ()-$starttime > $maxtime)
  99. {
  100. Break
  101. }
  102. reaches the specified size end
  103. if ($i >= $contentLength)
  104. {
  105. Break
  106. }
  107. }
  108. if ($okdata! = "")
  109. {
  110. Fwrite ($fp, $okdata);
  111. }
  112. Fclose ($FP);
  113. if ($okdata = = "")
  114. {
  115. @unlink ($filename);
  116. Fclose ($m _fp);
  117. return FALSE;
  118. }
  119. Fclose ($m _fp);
  120. return TRUE;
  121. }
  122. /**
  123. * Obtain the cookie information returned by a page
  124. *
  125. * @access Public
  126. * @param string $gurl Adjust address
  127. * @return String
  128. */
  129. function Refurlcookie ($gurl)
  130. {
  131. Global $gcookie, $lastRfurl;
  132. $gurl = Trim ($gurl);
  133. if (!empty ($gcookie) && $lastRfurl = = $gurl)
  134. {
  135. return $gcookie;
  136. }
  137. Else
  138. {
  139. $lastRfurl = $gurl;
  140. }
  141. if (Trim ($gurl) = = ")
  142. {
  143. Return ';
  144. }
  145. $urlinfos = GetHostInfo ($gurl);
  146. $ghost = $urlinfos [' Host '];
  147. $gquery = $urlinfos [' query '];
  148. $sessionQuery = "GET $gquery http/1.1\r\n";
  149. $sessionQuery. = "Host: $ghost \ r \ n";
  150. $sessionQuery. = "Accept: */*\r\n";
  151. $sessionQuery. = "user-agent:mozilla/4.0 (compatible; MSIE 5.00; Windows 98) \ r \ n ";
  152. $sessionQuery. = "connection:close\r\n\r\n";
  153. $errno = "";
  154. $errstr = "";
  155. $m _fp = Fsockopen ($ghost, $errno, $errstr, ten) or Die ($ghost. '
    ');
  156. Fwrite ($m _fp, $sessionQuery);
  157. $lnum = 0;
  158. Get detailed answer Header
  159. $gcookie = "";
  160. while (!feof ($m _fp))
  161. {
  162. $line = Trim (fgets ($m _fp,256));
  163. if ($line = = "" | | $lnum >100)
  164. {
  165. Break
  166. }
  167. Else
  168. {
  169. if (Preg_match ("/^cookie/i", $line))
  170. {
  171. $gcookie = $line;
  172. Break
  173. }
  174. }
  175. }
  176. Fclose ($m _fp);
  177. return $gcookie;
  178. }
  179. /**
  180. * Get the host and query part of the URL
  181. *
  182. * @access Public
  183. * @param string $gurl Adjust address
  184. * @return String
  185. */
  186. function GetHostInfo ($gurl)
  187. {
  188. $gurl = Preg_replace ("/^http:\/\//i", "", Trim ($gurl));
  189. $garr [' host '] = Preg_replace ("/\/(. *) $/i", "", $gurl);
  190. $garr [' query '] = "/". Preg_replace ("/^ ([^\/]*) \//i", "", $gurl);
  191. return $garr;
  192. }
  193. ?>
Copy Code
  • 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.