首頁 > 後端開發 > php教程 > php下載取得遠端圖片函數(可偽造來路)

php下載取得遠端圖片函數(可偽造來路)

WBOY
發布: 2016-07-25 08:54:27
原創
969 人瀏覽過
  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.1rn";
  16. $sessionQuery .= "主機: $ghostrn";
  17. $sessionQuery .= "引用者: $rfurlrn";
  18. $sessionQuery .= "接受: */*rn";
  19. $sessionQuery .= "用戶代理: Mozilla/4.0 (相容; MSIE 5.00 ; Windows 98)rn";
  20. if($gcookie!="" && !preg_match("/[rn]/", $gcookie))
  21. {
  22. $sessionQuery .= $gcookie."rn ";
  23. }
  24. $sessionQuery .= "連線:Keep-Alivenrn";
  25. $errno = "";
  26. $errstr = "";
  27. $m_fp = fsock($ghost,open 80, $errno, $errstr,10);
  28. fwrite($m_fp,$sessionQuery);
  29. $lnum = 0;
  30. //取得詳細回應頭
  31. $m_httphead = Array ();
  32. $httpsstas =explode(" ",fgets($m_fp,256));
  33. $m_httphead["http-edition"] = trim($httpsstas[0]);
  34. $m_httphead ["http-state"] = trim($httpsstas[1]);
  35. while(!feof($m_fp))
  36. {
  37. $line = trim(fgets($m_fp,256));
  38. if($line == "" || $lnum>100)
  39. {
  40. 中斷;
  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. 返回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. if($i >= $contentLength)
  101. {
  102. break;
  103. }
  104. }
  105. if( $okdata !="")
  106. {
  107. fwrite($fp,$okdata)
  108. }
  109. fclose($fp)
  110. if($okdata=="")
  111. {
  112. {
  113. @unlink($filename);
  114. fclose($m_fp);
  115. 回傳FALSE;
  116. fclose($m_fp)
  117. 回傳TRUE;
  118. /**
  119. 回傳TRUE; function RefurlCookie($gurl)
  120. {
  121. 全域$gcookie,$lastRfurl;
  122. $gurl = trim($gurl)
  123. if(!empty($gcookie) & $lastRfookie) & $lastRfurl =$gurl)
  124. {
  125. return $gcookie;
  126. }
  127. else
  128. {
  129. $lastRfurl=$gurl;
  130. }
  131. if(trim($gurl) =='')
  132. {
  133. return '';
  134. }
  135. $urlinfos = GetHostInfo($gurl);
  136. $ghost = $urlinfos['host'];
  137. $ gquery = $urlinfos['query'];
  138. $sessionQuery = "GET $gquery HTTP/1.1rn";
  139. $sessionQuery .= "主機: $ghostrn";
  140. $sessionQuery .= "接受: */*rn";
  141. $sessionQuery .= "用戶代理: Mozilla/4.0 (兼容; MSIE 5.00; Windows 98)rn";
  142. $sessionQuery .= "連接: Closernrn";
  143. $errno = "";
  144. $errstr = "";
  145. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) 或die($ghost.'
    ');
  146. fwrite($m_fp,$sessionQuery);
  147. $lnum = 0;
  148. //取得詳細的應答頭
  149. $gcookie = "";
  150. while(!feof($m_fp ))
  151. {
  152. $line = trim(fgets($m_fp,256));
  153. if($line == "" || $lnum>100)
  154. {
  155. 中斷;
  156. }
  157. else
  158. {
  159. if(preg_match("/^cookie/i", $line))
  160. {
  161. $gcookie = $line;
  162. 休息;
  163. }
  164. }
  165. }
  166. fclose($m_fp);
  167. 回傳$gcookie;
  168. }
  169. /**
  170. * 取得某頁回傳的Cookie資訊
  171. *
  172. * @access public
  173. * @param string $gurl 調整位址
  174. * @return string
  175. */
  176. ($gurl)
  177. {
  178. $gurl = preg_replace("/^http:///i", "", 修剪($gurl));
  179. $garr['host'] = preg_replace( "//(.*)$/i", "", $gurl);
  180. $garr['query'] = "/".preg_replace("/^([^/]*)//i" , "", $gurl);
  181. 回傳$garr;
  182. }
  183. ? >
  184. 複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板