Php download remote image retrieval function (forged path)

Source: Internet
Author: User
Tags php download
Php download remote image retrieval function (forged path)

  1. // Download and obtain remote images
  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 ";
  25. $ Errno = "";
  26. $ Errstr = "";
  27. $ M_fp = fsockopen ($ ghost, 80, $ errno, $ errstr, 10 );
  28. Fwrite ($ m_fp, $ sessionQuery );
  29. $ Lnum = 0;
  30. // Obtain the detailed response 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. If ($ v = 1)
  47. {
  48. $ Hvalue. = $ line [$ I];
  49. }
  50. If ($ line [$ I] = ":")
  51. {
  52. $ V = 1;
  53. }
  54. If ($ v = 0)
  55. {
  56. $ Hkey. = $ line [$ I];
  57. }
  58. }
  59. $ Hkey = trim ($ hkey );
  60. If ($ hkey! = "")
  61. {
  62. $ M_httphead [strtolower ($ hkey)] = trim ($ hvalue );
  63. }
  64. }
  65. // Analyze the returned Records
  66. If (preg_match ("/^ 3/", $ m_httphead ["http-state"])
  67. {
  68. If (isset ($ m_httphead ["location"]) & $ JumpCount <3)
  69. {
  70. $ JumpCount ++;
  71. DownImageKeep ($ gurl, $ rfurl, $ filename, $ gcookie, $ JumpCount );
  72. }
  73. Else
  74. {
  75. Return FALSE;
  76. }
  77. }
  78. If (! Preg_match ("/^ 2/", $ m_httphead ["http-state"])
  79. {
  80. Return FALSE;
  81. }
  82. If (! Isset ($ m_httphead ))
  83. {
  84. Return FALSE;
  85. }
  86. $ ContentLength = $ m_httphead ['content-length'];
  87. // Save the file
  88. $ Fp = fopen ($ filename, "w") or die ("writing File: {$ filename} failed! ");
  89. $ I = 0;
  90. $ Okdata = "";
  91. $ Starttime = time ();
  92. While (! Feof ($ m_fp ))
  93. {
  94. $ Okdata. = fgetc ($ m_fp );
  95. $ I ++;
  96. // Timeout ends
  97. If (time ()-$ starttime> $ maxtime)
  98. {
  99. Break;
  100. }
  101. // End with the specified size
  102. If ($ I >=$ contentLength)
  103. {
  104. Break;
  105. }
  106. }
  107. If ($ okdata! = "")
  108. {
  109. Fwrite ($ fp, $ okdata );
  110. }
  111. Fclose ($ fp );
  112. If ($ okdata = "")
  113. {
  114. @ Unlink ($ filename );
  115. Fclose ($ m_fp );
  116. Return FALSE;
  117. }
  118. Fclose ($ m_fp );
  119. Return TRUE;
  120. }
  121. /**
  122. * Obtain the Cookie information returned from a page.
  123. *
  124. * @ Access public
  125. * @ Param string $ gurl adjust the address
  126. * @ Return string
  127. */
  128. Function RefurlCookie ($ gurl)
  129. {
  130. Global $ gcookie, $ lastRfurl;
  131. $ Gurl = trim ($ gurl );
  132. If (! Empty ($ gcookie) & $ lastRfurl ==$ gurl)
  133. {
  134. Return $ gcookie;
  135. }
  136. Else
  137. {
  138. $ LastRfurl = $ gurl;
  139. }
  140. If (trim ($ gurl) = '')
  141. {
  142. Return '';
  143. }
  144. $ Urlinfos = GetHostInfo ($ gurl );
  145. $ Ghost = $ urlinfos ['host'];
  146. $ Gquery = $ urlinfos ['query'];
  147. $ SessionQuery = "GET $ gquery HTTP/1.1 \ r \ n ";
  148. $ SessionQuery. = "Host: $ ghost \ r \ n ";
  149. $ SessionQuery. = "Accept: */* \ r \ n ";
  150. $ SessionQuery. = "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98) \ r \ n ";
  151. $ SessionQuery. = "Connection: Close \ r \ n ";
  152. $ Errno = "";
  153. $ Errstr = "";
  154. $ M_fp = fsockopen ($ ghost, 80, $ errno, $ errstr, 10) or die ($ ghost .'
    ');
  155. Fwrite ($ m_fp, $ sessionQuery );
  156. $ Lnum = 0;
  157. // Obtain the detailed response header
  158. $ Gcookie = "";
  159. While (! Feof ($ m_fp ))
  160. {
  161. $ Line = trim (fgets ($ m_fp, 256 ));
  162. If ($ line = "" ||$ lnum> 100)
  163. {
  164. Break;
  165. }
  166. Else
  167. {
  168. If (preg_match ("/^ cookie/I", $ line ))
  169. {
  170. $ Gcookie = $ line;
  171. Break;
  172. }
  173. }
  174. }
  175. Fclose ($ m_fp );
  176. Return $ gcookie;
  177. }
  178. /**
  179. * Obtain the host and query parts of the website.
  180. *
  181. * @ Access public
  182. * @ Param string $ gurl adjust the address
  183. * @ Return string
  184. */
  185. Function GetHostInfo ($ gurl)
  186. {
  187. $ Gurl = preg_replace ("/^ http: \\// I", "", trim ($ gurl ));
  188. $ Garr ['host'] = preg_replace ("// (. *) $/I", "", $ gurl );
  189. $ Garr ['query'] = "/". preg_replace ("/^ ([^ \/] *) \ // I", "", $ gurl );
  190. Return $ garr;
  191. }
  192. ?>

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.