首页 > 后端开发 > php教程 > php下载获取远程图片函数(可伪造来路)

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

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


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板