国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP下載遠程文件類源碼,帶詳細注釋,還支持斷點續傳

2024-09-04 11:45:10
字體:
來源:轉載
供稿:網友

網站經常要用到下載遠程圖片的功能,有時還要下載.doc文檔,所以就用php開發了一個下載遠程文件的類,這個類支持斷點續傳下載,源代碼中有具體的注釋及使用說明案例.

程序主要是使用 HTTP 協議下載文件,HTTP1.1協議必須指定文檔結束后關閉鏈接,否則讀取文檔時無法使用feof判斷結束,可以有兩種使用方法,具體請下載查看源碼,代碼如下:

  1. <?php 
  2. /** 
  3.  * 下載遠程文件類支持斷點續傳  
  4.  */ 
  5. class HttpDownload { 
  6.     private $m_url = ""
  7.      private $m_urlpath = ""
  8.      private $m_scheme = "http"
  9.      private $m_host = ""
  10.      private $m_port = "80"
  11.      private $m_user = ""
  12.      private $m_pass = ""
  13.      private $m_path = "/"
  14.      private $m_query = ""
  15.      private $m_fp = ""
  16.      private $m_error = ""
  17.     private $m_httphead = "" ; 
  18.     private $m_html = ""
  19.  
  20.     /** 
  21.      * 初始化  
  22.      */ 
  23.     public function PrivateInit($url){ 
  24.         $urls = ""
  25.         $urls = @parse_url($url); 
  26.         $this->m_url = $url
  27.         if(is_array($urls)) { 
  28.             $this->m_host = $urls["host"]; 
  29.             if(!emptyempty($urls["scheme"])) $this->m_scheme = $urls["scheme"]; 
  30.             if(!emptyempty($urls["user"])) $this->m_user = $urls["user"]; 
  31.             if(!emptyempty($urls["pass"])) $this->m_pass = $urls["pass"]; 
  32.             if(!emptyempty($urls["port"])) $this->m_port = $urls["port"]; 
  33.             if(!emptyempty($urls["path"])) $this->m_path = $urls["path"]; 
  34.             $this->m_urlpath = $this->m_path; 
  35.             if(!emptyempty($urls["query"])) { 
  36.                  $this->m_query = $urls["query"]; 
  37.                  $this->m_urlpath .= "?".$this->m_query; 
  38.              } 
  39.           } 
  40.     } 
  41.  
  42.     /** 
  43.     * 打開指定網址 
  44.     */ 
  45.     function OpenUrl($url) { 
  46.         #重設各參數 
  47.         $this->m_url = ""
  48.         $this->m_urlpath = ""
  49.         $this->m_scheme = "http"
  50.         $this->m_host = ""
  51.         $this->m_port = "80"
  52.         $this->m_user = ""
  53.         $this->m_pass = ""
  54.         $this->m_path = "/"
  55.         $this->m_query = ""
  56.         $this->m_error = ""
  57.         $this->m_httphead = "" ; 
  58.         $this->m_html = ""
  59.         $this->Close(); 
  60.         #初始化系統 
  61.         $this->PrivateInit($url); 
  62.         $this->PrivateStartSession(); 
  63.     } 
  64.  
  65.     /** 
  66.     * 獲得某操作錯誤的原因 
  67.     */ 
  68.     public function printError() { 
  69.         echo "錯誤信息:".$this->m_error; 
  70.         echo "具體返回頭:<br>"
  71.         foreach($this->m_httphead as $k=>$v) {  
  72.             echo "$k => $v <br>rn";  
  73.         } 
  74.     } 
  75.  
  76.     /** 
  77.     * 判別用Get方法發送的頭的應答結果是否正確 
  78.     */ 
  79.     public function IsGetOK() { 
  80.         ifereg("^2",$this->GetHead("http-state")) ) {  
  81.             return true;  
  82.         } else { 
  83.             $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br>"
  84.             return false; 
  85.         } 
  86.     } 
  87.      
  88.     /** 
  89.     * 看看返回的網頁是否是text類型 
  90.     */ 
  91.     public function IsText() { 
  92.         if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {  
  93.             return true;  
  94.         } else { 
  95.             $this->m_error .= "內容為非文本類型<br>"
  96.             return false; 
  97.         } 
  98.     } 
  99.     /** 
  100.     * 判斷返回的網頁是否是特定的類型 
  101.     */ 
  102.     public function IsContentType($ctype) { 
  103.         if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {  
  104.             return true;  
  105.         } else { 
  106.             $this->m_error .= "類型不對 ".$this->GetHead("content-type")."<br>"
  107.             return false; 
  108.         } 
  109.     } 
  110.      
  111.     /** 
  112.     * 用 HTTP 協議下載文件 
  113.     */ 
  114.     public function SaveToBin($savefilename) { 
  115.         if (!$this->IsGetOK()) return false; 
  116.         if (@feof($this->m_fp)) {  
  117.             $this->m_error = "連接已經關閉!";  
  118.             return false;  
  119.         } 
  120.         $fp = fopen($savefilename,"w"or die("寫入文件 $savefilename 失敗!"); 
  121.         while (!feof($this->m_fp)) { 
  122.             @fwrite($fp,fgets($this->m_fp,256)); 
  123.         } 
  124.         @fclose($this->m_fp); 
  125.         return true; 
  126.     } 
  127.      
  128.     /** 
  129.     * 保存網頁內容為 Text 文件 
  130.     */ 
  131.     public function SaveToText($savefilename) { 
  132.         if ($this->IsText()) { 
  133.             $this->SaveBinFile($savefilename); 
  134.         } else { 
  135.             return ""
  136.         } 
  137.     } 
  138.      
  139.     /** 
  140.     * 用 HTTP 協議獲得一個網頁的內容 
  141.     */ 
  142.     public function GetHtml() { 
  143.         if (!$this->IsText()) return ""
  144.         if ($this->m_html!=""return $this->m_html; 
  145.         if (!$this->m_fp||@feof($this->m_fp)) return ""
  146.         while(!feof($this->m_fp)) { 
  147.             $this->m_html .= fgets($this->m_fp,256); 
  148.         } 
  149.         @fclose($this->m_fp); 
  150.         return $this->m_html; 
  151.     } 
  152.      
  153.     /** 
  154.     * 開始 HTTP 會話 
  155.     */ 
  156.     public function PrivateStartSession() { 
  157.         if (!$this->PrivateOpenHost()) { 
  158.             $this->m_error .= "打開遠程主機出錯!"
  159.             return false; 
  160.         } 
  161.         if ($this->GetHead("http-edition")=="HTTP/1.1") { 
  162.             $httpv = "HTTP/1.1"
  163.         } else { 
  164.             $httpv = "HTTP/1.0"
  165.         } 
  166.         fputs($this->m_fp,"GET ".$this->m_urlpath." $httpvrn"); 
  167.         fputs($this->m_fp,"Host: ".$this->m_host."rn"); 
  168.         fputs($this->m_fp,"Accept: */*rn"); 
  169.         fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)rn"); 
  170.         #HTTP1.1協議必須指定文檔結束后關閉鏈接,否則讀取文檔時無法使用feof判斷結束 
  171.         if ($httpv=="HTTP/1.1") { 
  172.             fputs($this->m_fp,"Connection: Closernrn"); 
  173.         } else { 
  174.             fputs($this->m_fp,"rn"); 
  175.         } 
  176.         $httpstas = fgets($this->m_fp,256); 
  177.         $httpstas = split(" ",$httpstas); 
  178.         $this->m_httphead["http-edition"] = trim($httpstas[0]); 
  179.         $this->m_httphead["http-state"] = trim($httpstas[1]); 
  180.         $this->m_httphead["http-describe"] = ""
  181.         for ($i=2;$i<count($httpstas);$i++) { 
  182.             $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]); 
  183.         } 
  184.         while (!feof($this->m_fp)) { 
  185.             $line = str_replace(""","",trim(fgets($this->m_fp,256))); 
  186.             if($line == ""break
  187.             if (ereg(":",$line)) { 
  188.                 $lines = split(":",$line); 
  189.                 $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]); 
  190.             } 
  191.         } 
  192.     } 
  193.      
  194.     /** 
  195.     * 獲得一個Http頭的值 
  196.     */ 
  197.     public function GetHead($headname) { 
  198.         $headname = strtolower($headname); 
  199.         if (isset($this->m_httphead[$headname])) { 
  200.             return $this->m_httphead[$headname]; 
  201.         } else { 
  202.             return ""
  203.         } 
  204.     } 
  205.      
  206.     /** 
  207.     * 打開連接 
  208.     */ 
  209.     public function PrivateOpenHost() { 
  210.         if ($this->m_host==""return false; 
  211.         $this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10); 
  212.         if (!$this->m_fp){ 
  213.             $this->m_error = $errstr
  214.             return false; 
  215.         } else { 
  216.             return true; 
  217.         } 
  218.     } 
  219.      
  220.     /** 
  221.     * 關閉連接 
  222.     */ 
  223.     public function Close(){ 
  224.         @fclose($this->m_fp); 
  225.     } 
  226.  
  227. #兩種使用方法,分別如下: 
  228.  
  229. #打開網頁 
  230. $httpdown = new HttpDownload(); 
  231. $httpdown->OpenUrl("http://www.google.com.hk"); 
  232. echo $httpdown->GetHtml(); 
  233. $httpdown->Close(); 
  234.  
  235.  
  236. #下載文件 
  237. $file = new HttpDownload(); # 實例化類 
  238. $file->OpenUrl("http://www.survivalescaperooms.com/cn/lit/an/rust020/rust020.pdf"); # 遠程文件地址 
  239. $file->SaveToBin("rust020.pdf"); # 保存路徑及文件名 
  240. $file->Close(); # 釋放資源 
  241. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 七台河市| 长岭县| 航空| 仙游县| 绩溪县| 墨玉县| 承德县| 乐至县| 慈利县| 浦县| 北宁市| 故城县| 济宁市| 江安县| 呼玛县| 临澧县| 玛曲县| 敖汉旗| 阳曲县| 图木舒克市| 苏尼特右旗| 濉溪县| 长沙市| 海城市| 康保县| 合作市| 衡南县| 永年县| 台南市| 娄底市| 华蓥市| 梅河口市| 宁夏| 贺兰县| 乌兰察布市| 青铜峡市| 夏邑县| 巴彦淖尔市| 元朗区| 建瓯市| 安西县|