当前位置:Gxlcms > PHP教程 > php下载获取远程图片函数(可伪造来路)

php下载获取远程图片函数(可伪造来路)

时间:2021-07-01 10:21:17 帮助过:9人阅读

  1. //下载获取远程图片
  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, 80, $errno, $errstr,10);
  28. fwrite($m_fp,$sessionQuery);
  29. $lnum = 0;
  30. //获取详细应答头
  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. //分析返回记录
  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. //保存文件
  88. $fp = fopen($filename,"w") or die("写入文件:{$filename} 失败!");
  89. $i=0;
  90. $okdata = "";
  91. $starttime = time();
  92. while(!feof($m_fp))
  93. {
  94. $okdata .= fgetc($m_fp);
  95. $i++;
  96. //超时结束
  97. if(time()-$starttime>$maxtime)
  98. {
  99. break;
  100. }
  101. //到达指定大小结束
  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. * 获得某页面返回的Cookie信息
  123. *
  124. * @access public
  125. * @param string $gurl 调整地址
  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\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. //获取详细应答头
  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. * 获得网址的host和query部份
  180. *
  181. * @access public
  182. * @param string $gurl 调整地址
  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. ?>

人气教程排行