当前位置:首页 > PHP教程 > php上传下载 > 列表

PHP下载远程文件类源码,带详细注释,还支持断点续传

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-09 13:34:41 浏览: 评论:0 

网站经常要用到下载远程图片的功能,有时还要下载.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.phpfensi.com/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址 
  239. $file->SaveToBin("rust020.pdf"); # 保存路径及文件名 
  240. $file->Close(); # 释放资源 
  241. ?>

Tags: PHP下载文件 PHP远程文件

分享到: