-
-
//下載取得遠端圖片
- function DownImageKeep($gurl, $rfurl, $filename, $gcookie="", $ JumpCount =0, $maxtime=30)
- {
- $urlinfos = GetHostInfo($gurl);
- $ghost = trim($urlinfos['host']);
- if($ghost== '')
- {
- return FALSE;
- }
- $gquery = $urlinfos['query'];
- if($gcookie=="" && !empty($rfurl))
- {
- $gcookie = RefurlCookie($rfurl);
- }
- $sessionQuery = "GET $gquery HTTP/1.1rn";
- $sessionQuery .= "主機: $ghostrn";
- $sessionQuery .= "引用者: $rfurlrn";
- $sessionQuery .= "接受: */*rn";
- $sessionQuery .= "用戶代理: Mozilla/4.0 (相容; MSIE 5.00 ; Windows 98)rn";
- if($gcookie!="" && !preg_match("/[rn]/", $gcookie))
- {
- $sessionQuery .= $gcookie."rn ";
- }
- $sessionQuery .= "連線:Keep-Alivenrn";
- $errno = "";
- $errstr = "";
- $m_fp = fsock($ghost,open 80, $errno, $errstr,10);
- fwrite($m_fp,$sessionQuery);
- $lnum = 0;
-
- //取得詳細回應頭
- $m_httphead = Array ();
- $httpsstas =explode(" ",fgets($m_fp,256));
- $m_httphead["http-edition"] = trim($httpsstas[0]);
- $m_httphead ["http-state"] = trim($httpsstas[1]);
- while(!feof($m_fp))
- {
- $line = trim(fgets($m_fp,256));
- if($line == "" || $lnum>100)
- {
- 中斷;
- }
- $hkey = "";
- $hvalue = "";
- $v = 0;
- for($i=0; $i {
- if($v==1)
- {
- $hvalue .= $line [$i];
- }
- if($line[$i]==":")
- {
- $v = 1;
- }
- if($v==0)
- {
- $hkey .= $line[$i];
- }
- }
- $hkey = trim($hkey);
- if($hkey!="")
- {
- $m_httphead[strtolower($hkey)] = trim($hvalue);
- }
- }
-
- // 分析回傳記錄
- if(preg_match("/^3/", $m_httphead["http-state"]))
- {
- if(isset($m_httphead["location"]) && $JumpCount {
- $JumpCount ;
- DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
- }
- else
- {
- 返回FALSE ;
- }
- }
- if(!preg_match("/^2/", $m_httphead["http-state"]))
- {
- return FALSE;
- }
- if(!isset($m_httphead))
- {
- return FALSE;
- }
- $contentLength = $m_httphead['content-length'];
-
- //保存🎜> //保存檔案
- $fp = fopen($filename,"w") or die("寫入檔案:{$filename} 失敗!");
- $i=0;
- $okdata = "";
- $starttime = time();
- while(!feof($m_fp))
- {
- $ okdata .= fgetc($m_fp);
- $i ;
-
- // 超時結束
- if(time()-$starttime>$maxtime)
- {
- break; 🎜 > }
-
- //到達指定大小結束
- if($i >= $contentLength)
- {
- break;
- }
- }
- if( $okdata !="")
- {
- fwrite($fp,$okdata)
- }
- fclose($fp)
- if($okdata=="")
- {
- {
- @unlink($filename);
- fclose($m_fp);
- 回傳FALSE;
- fclose($m_fp)
- 回傳TRUE;
-
- /**
- 回傳TRUE; function RefurlCookie($gurl)
- {
- 全域$gcookie,$lastRfurl;
- $gurl = trim($gurl)
- if(!empty($gcookie) & $lastRfookie) & $lastRfurl =$gurl)
- {
- return $gcookie;
- }
- else
- {
- $lastRfurl=$gurl;
- }
- if(trim($gurl) =='')
- {
- return '';
- }
- $urlinfos = GetHostInfo($gurl);
- $ghost = $urlinfos['host'];
- $ gquery = $urlinfos['query'];
- $sessionQuery = "GET $gquery HTTP/1.1rn";
- $sessionQuery .= "主機: $ghostrn";
- $sessionQuery .= "接受: */*rn";
- $sessionQuery .= "用戶代理: Mozilla/4.0 (兼容; MSIE 5.00; Windows 98)rn";
- $sessionQuery .= "連接: Closernrn";
- $errno = "";
- $errstr = "";
- $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) 或die($ghost.'
');
- fwrite($m_fp,$sessionQuery);
- $lnum = 0;
-
- //取得詳細的應答頭
- $gcookie = "";
- while(!feof($m_fp ))
- {
- $line = trim(fgets($m_fp,256));
- if($line == "" || $lnum>100)
- {
- 中斷;
- }
- else
- {
- if(preg_match("/^cookie/i", $line))
- {
- $gcookie = $line;
- 休息;
- }
- }
- }
- fclose($m_fp);
- 回傳$gcookie;
- }
-
- /**
- * 取得某頁回傳的Cookie資訊
- *
- * @access public
- * @param string $gurl 調整位址
- * @return string
- */
- ($gurl)
- {
- $gurl = preg_replace("/^http:///i", "", 修剪($gurl));
- $garr['host'] = preg_replace( "//(.*)$/i", "", $gurl);
- $garr['query'] = "/".preg_replace("/^([^/]*)//i" , "", $gurl);
- 回傳$garr;
- }
- ? >
-
-
- 複製程式碼
-
-
-
|