PHP download remote image function example (forged source)

WBOY
Release: 2016-07-25 08:53:05
Original
883 people have browsed it
  1. /**
  2. * @Function: Download remote pictures
  3. * @bbs.it-home.org
  4. */
  5. function DownImageKeep($gurl, $rfurl, $filename, $gcookie="", $JumpCount=0, $maxtime=30)
  6. {
  7. $urlinfos = GetHostInfo($gurl);
  8. $ghost = trim($urlinfos['host']);
  9. if($ghost=='')
  10. {
  11. return FALSE;
  12. }
  13. $gquery = $urlinfos['query'];
  14. if($gcookie=="" && !empty($rfurl))
  15. {
  16. $gcookie = RefurlCookie($rfurl);
  17. }
  18. $sessionQuery = "GET $gquery HTTP/1.1rn";
  19. $sessionQuery .= "Host: $ghostrn";
  20. $sessionQuery .= "Referer: $rfurlrn";
  21. $sessionQuery .= "Accept: */*rn";
  22. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn";
  23. if($gcookie!="" && !preg_match("/[rn]/", $gcookie))
  24. {
  25. $sessionQuery .= $gcookie."rn";
  26. }
  27. $sessionQuery .= "Connection: Keep-Alivernrn";
  28. $errno = "";
  29. $errstr = "";
  30. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10);
  31. fwrite($m_fp,$sessionQuery);
  32. $lnum = 0;
  33. //获取详细应答头
  34. $m_httphead = Array();
  35. $httpstas = explode(" ",fgets($m_fp,256));
  36. $m_httphead["http-edition"] = trim($httpstas[0]);
  37. $m_httphead["http-state"] = trim($httpstas[1]);
  38. while(!feof($m_fp))
  39. {
  40. $line = trim(fgets($m_fp,256));
  41. if($line == "" || $lnum>100)
  42. {
  43. break;
  44. }
  45. $hkey = "";
  46. $hvalue = "";
  47. $v = 0;
  48. for($i=0; $i {
  49. if($v==1)
  50. {
  51. $hvalue .= $line[$i];
  52. }
  53. if($line[$i]==":")
  54. {
  55. $v = 1;
  56. }
  57. if($v==0)
  58. {
  59. $hkey .= $line[$i];
  60. }
  61. }
  62. $hkey = trim($hkey);
  63. if($hkey!="")
  64. {
  65. $m_httphead[strtolower($hkey)] = trim($hvalue);
  66. }
  67. }
  68. //分析返回记录
  69. if(preg_match("/^3/", $m_httphead["http-state"]))
  70. {
  71. if(isset($m_httphead["location"]) && $JumpCount<3)
  72. {
  73. $JumpCount++;
  74. DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
  75. }
  76. else
  77. {
  78. return FALSE;
  79. }
  80. }
  81. if(!preg_match("/^2/", $m_httphead["http-state"]))
  82. {
  83. return FALSE;
  84. }
  85. if(!isset($m_httphead))
  86. {
  87. return FALSE;
  88. }
  89. $contentLength = $m_httphead['content-length'];
  90. //保存文件
  91. $fp = fopen($filename,"w") or die("写入文件:{$filename} 失败!");
  92. $i=0;
  93. $okdata = "";
  94. $starttime = time();
  95. while(!feof($m_fp))
  96. {
  97. $okdata .= fgetc($m_fp);
  98. $i++;
  99. //超时结束
  100. if(time()-$starttime>$maxtime)
  101. {
  102. break;
  103. }
  104. //到达指定大小结束
  105. if($i >= $contentLength)
  106. {
  107. break;
  108. }
  109. }
  110. if($okdata!="")
  111. {
  112. fwrite($fp,$okdata);
  113. }
  114. fclose($fp);
  115. if($okdata=="")
  116. {
  117. @unlink($filename);
  118. fclose($m_fp);
  119. return FALSE;
  120. }
  121. fclose($m_fp);
  122. return TRUE;
  123. }
  124. /**
  125. * Get the cookie information returned by a page
  126. *
  127. * @access public
  128. * @param string $gurl Adjust the address
  129. * @return string
  130. */
  131. function RefurlCookie($gurl)
  132. {
  133. global $gcookie,$lastRfurl;
  134. $gurl = trim($gurl);
  135. if(!empty($gcookie) && $lastRfurl==$gurl)
  136. {
  137. return $gcookie;
  138. }
  139. else
  140. {
  141. $lastRfurl=$gurl;
  142. }
  143. if(trim($gurl)=='')
  144. {
  145. return '';
  146. }
  147. $urlinfos = GetHostInfo($gurl);
  148. $ghost = $urlinfos['host'];
  149. $gquery = $urlinfos['query'];
  150. $sessionQuery = "GET $gquery HTTP/1.1rn";
  151. $sessionQuery .= "Host: $ghostrn";
  152. $sessionQuery .= "Accept: */*rn";
  153. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn";
  154. $sessionQuery .= "Connection: Closernrn";
  155. $errno = "";
  156. $errstr = "";
  157. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.'
    ');
  158. fwrite($m_fp,$sessionQuery);
  159. $lnum = 0;
  160. //获取详细应答头
  161. $gcookie = "";
  162. while(!feof($m_fp))
  163. {
  164. $line = trim(fgets($m_fp,256));
  165. if($line == "" || $lnum>100)
  166. {
  167. break;
  168. }
  169. else
  170. {
  171. if(preg_match("/^cookie/i", $line))
  172. {
  173. $gcookie = $line;
  174. break;
  175. }
  176. }
  177. }
  178. fclose($m_fp);
  179. return $gcookie;
  180. }
  181. /**
  182. * Get the host and query parts of the URL
  183. *
  184. * @access public
  185. * @param string $gurl Adjust the address
  186. * @return string
  187. */
  188. function GetHostInfo($gurl)
  189. {
  190. $gurl = preg_replace("/^http:///i", "", trim($gurl));
  191. $garr['host'] = preg_replace("//(.*)$/i", "", $gurl);
  192. $garr['query'] = "/".preg_replace("/^([^/]*)//i", "", $gurl);
  193. return $garr;
  194. }
  195. ?>
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!