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

php下載遠端圖片函數範例(偽造來路)

WBOY
發布: 2016-07-25 08:53:05
原創
975 人瀏覽過
  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.1rn";
  19. $sessionQuery .= "主機: $ghostrn ";
  20. $sessionQuery .= "引用者: $rfurlrn";
  21. $sessionQuery .= "接受: */*rn";
  22. $sessionQuery .= "用戶代理: Mozilla/4.0 (相容; MSIE 5.00; Windows 98)rn";
  23. if($gcookie!="" && !preg_match("/[rn]/", $gcookie))
  24. {
  25. $sessionQuery .= $gcookie. "rn";
  26. }
  27. $sessionQuery .= "連接:Keep-Alivenrn";
  28. $errno = "";
  29. $errstr = "";
  30. $m_fp = fsockopen($ ghost, 80, $errno, $errstr,10);
  31. fwrite($m_fp,$sessionQuery);
  32. $lnum = 0;
  33. //取得詳細答案= Array();
  34. $httpsstas =explode(" ",fgets($m_fp,256));
  35. $m_httphead["http-edition"] = trim($httpsstas[0]);
  36. $m_httphead["http-state"] = trim($httpsstas[1]);
  37. while(!feof($m_fp))
  38. {
  39. $line = trim(fgets($m_fp,256) );
  40. if($line == "" || $lnum>100)
  41. {
  42. 中斷;
  43. }
  44. $hkey = "";
  45. $hvalue = "" ;
  46. $v = 0;
  47. for($i=0; $i {
  48. if($v==1)
  49. {
  50. $hvalue .= $line [$i];
  51. }
  52. if($line[$i]==":")
  53. {
  54. $v = 1;
  55. }
  56. if($v==0)
  57. {
  58. $hkey .= $line[$i];
  59. }
  60. }
  61. $hkey = trim($hkey);
  62. if($hkey!="")
  63. {
  64. $m_httphead[strtolower($hkey)] = trim($hvalue);
  65. }
  66. }
  67. / / 分析回傳記錄
  68. if(preg_match("/^3/", $m_httphead["http-state"]))
  69. {
  70. if(isset($m_httphead["location"]) && $ JumpCount {
  71. $JumpCount ;
  72. DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
  73. }
  74. else
  75. {
  76. {
  77. 返回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. / /儲存檔案 $fp = fopen($filename,"w") or die("寫入檔案:{$filename} 失敗!");
  90. $i=0;
  91. $okdata = "";
  92. $starttime = time();
  93. while(!feof($m_fp))
  94. {
  95. $ okdata .= fgetc($m_fp);
  96. $i ;
  97. // 超時結束
  98. if(time()-$starttime>$maxtime)
  99. {
  100. break; 🎜 > }
  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. {
  115. @unlink($filename);
  116. fclose($m_fp);
  117. 回傳FALSE;
  118. fclose($m_fp)
  119. 回傳TRUE;
  120. /**
  121. 回傳TRUE; function RefurlCookie($gurl)
  122. {
  123. 全域$gcookie,$lastRfurl;
  124. $gurl = trim($gurl)
  125. if(!empty($gcookie) & $lastRfookie) & $lastRfurl =$gurl)
  126. {
  127. return $gcookie;
  128. }
  129. else
  130. {
  131. $lastRfurl=$gurl;
  132. }
  133. if(trim($gurl) =='')
  134. {
  135. return '';
  136. }
  137. $urlinfos = GetHostInfo($gurl);
  138. $ghost = $urlinfos['host'];
  139. $ gquery = $urlinfos['query'];
  140. $sessionQuery = "GET $gquery HTTP/1.1rn";
  141. $sessionQuery .= "主機: $ghostrn";
  142. $sessionQuery .= "接受: */*rn";
  143. $sessionQuery .= "用戶代理: Mozilla/4.0 (兼容; MSIE 5.00; Windows 98)rn";
  144. $sessionQuery .= "連接: Closernrn";
  145. $errno = "";
  146. $errstr = "";
  147. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) 或die($ghost.'
    ');
  148. fwrite($m_fp,$sessionQuery);
  149. $lnum = 0;
  150. //取得詳細的應答頭
  151. $gcookie = "";
  152. while(!feof($m_fp ))
  153. {
  154. $line = trim(fgets($m_fp,256));
  155. if($line == "" || $lnum>100)
  156. {
  157. 中斷;
  158. }
  159. else
  160. {
  161. if(preg_match("/^cookie/i", $line))
  162. {
  163. $gcookie = $line;
  164. 休息;
  165. }
  166. }
  167. }
  168. fclose($m_fp);
  169. 回傳$gcookie;
  170. }
  171. /**
  172. * 取得某頁回傳的Cookie資訊
  173. *
  174. * @access public
  175. * @param string $gurl 調整位址
  176. * @return string
  177. */
  178. ($gurl)
  179. {
  180. $gurl = preg_replace("/^http:///i", "", 修剪($gurl));
  181. $garr['host'] = preg_replace( "//(.*)$/i", "", $gurl);
  182. $garr['query'] = "/".preg_replace("/^([^/]*)//i" , "", $gurl);
  183. 回傳$garr;
  184. }
  185. ? >
  186. 複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板