欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php下载远程图片函数示例(伪造来路)

程序员文章站 2022-04-07 09:19:24
...
  1. /**
  2. * @功能:下载远程图片
  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.1\r\n";
  19. $sessionQuery .= "Host: $ghost\r\n";
  20. $sessionQuery .= "Referer: $rfurl\r\n";
  21. $sessionQuery .= "Accept: */*\r\n";
  22. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
  23. if($gcookie!="" && !preg_match("/[\r\n]/", $gcookie))
  24. {
  25. $sessionQuery .= $gcookie."\r\n";
  26. }
  27. $sessionQuery .= "Connection: Keep-Alive\r\n\r\n";
  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 {
  72. $JumpCount++;
  73. DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
  74. }
  75. else
  76. {
  77. return FALSE;
  78. }
  79. }
  80. if(!preg_match("/^2/", $m_httphead["http-state"]))
  81. {
  82. return FALSE;
  83. }
  84. if(!isset($m_httphead))
  85. {
  86. return FALSE;
  87. }
  88. $contentLength = $m_httphead['content-length'];
  89. //保存文件
  90. $fp = fopen($filename,"w") or die("写入文件:{$filename} 失败!");
  91. $i=0;
  92. $okdata = "";
  93. $starttime = time();
  94. while(!feof($m_fp))
  95. {
  96. $okdata .= fgetc($m_fp);
  97. $i++;
  98. //超时结束
  99. if(time()-$starttime>$maxtime)
  100. {
  101. break;
  102. }
  103. //到达指定大小结束
  104. if($i >= $contentLength)
  105. {
  106. break;
  107. }
  108. }
  109. if($okdata!="")
  110. {
  111. fwrite($fp,$okdata);
  112. }
  113. fclose($fp);
  114. if($okdata=="")
  115. {
  116. @unlink($filename);
  117. fclose($m_fp);
  118. return FALSE;
  119. }
  120. fclose($m_fp);
  121. return TRUE;
  122. }
  123. /**
  124. * 获得某页面返回的Cookie信息
  125. *
  126. * @access public
  127. * @param string $gurl 调整地址
  128. * @return string
  129. */
  130. function RefurlCookie($gurl)
  131. {
  132. global $gcookie,$lastRfurl;
  133. $gurl = trim($gurl);
  134. if(!empty($gcookie) && $lastRfurl==$gurl)
  135. {
  136. return $gcookie;
  137. }
  138. else
  139. {
  140. $lastRfurl=$gurl;
  141. }
  142. if(trim($gurl)=='')
  143. {
  144. return '';
  145. }
  146. $urlinfos = GetHostInfo($gurl);
  147. $ghost = $urlinfos['host'];
  148. $gquery = $urlinfos['query'];
  149. $sessionQuery = "GET $gquery HTTP/1.1\r\n";
  150. $sessionQuery .= "Host: $ghost\r\n";
  151. $sessionQuery .= "Accept: */*\r\n";
  152. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
  153. $sessionQuery .= "Connection: Close\r\n\r\n";
  154. $errno = "";
  155. $errstr = "";
  156. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.'
    ');
  157. fwrite($m_fp,$sessionQuery);
  158. $lnum = 0;
  159. //获取详细应答头
  160. $gcookie = "";
  161. while(!feof($m_fp))
  162. {
  163. $line = trim(fgets($m_fp,256));
  164. if($line == "" || $lnum>100)
  165. {
  166. break;
  167. }
  168. else
  169. {
  170. if(preg_match("/^cookie/i", $line))
  171. {
  172. $gcookie = $line;
  173. break;
  174. }
  175. }
  176. }
  177. fclose($m_fp);
  178. return $gcookie;
  179. }
  180. /**
  181. * 获得网址的host和query部份
  182. *
  183. * @access public
  184. * @param string $gurl 调整地址
  185. * @return string
  186. */
  187. function GetHostInfo($gurl)
  188. {
  189. $gurl = preg_replace("/^http:\/\//i", "", trim($gurl));
  190. $garr['host'] = preg_replace("/\/(.*)$/i", "", $gurl);
  191. $garr['query'] = "/".preg_replace("/^([^\/]*)\//i", "", $gurl);
  192. return $garr;
  193. }
  194. ?>
复制代码