PHP实现的下载远程文件类定义与用法示例


Posted in PHP onJuly 05, 2017

本文实例讲述了PHP实现的下载远程文件类定义与用法。分享给大家供大家参考,具体如下:

<?php
/**
 * 下载远程文件类支持断点续传
 */
class HttpDownload {
  private $m_url = "";
  private $m_urlpath = "";
  private $m_scheme = "http";
  private $m_host = "";
  private $m_port = "80";
  private $m_user = "";
  private $m_pass = "";
  private $m_path = "/";
  private $m_query = "";
  private $m_fp = "";
  private $m_error = "";
  private $m_httphead = "" ;
  private $m_html = "";
  /**
   * 初始化
   */
  public function PrivateInit($url){
    $urls = "";
    $urls = @parse_url($url);
    $this->m_url = $url;
    if(is_array($urls)) {
      $this->m_host = $urls["host"];
      if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
      if(!empty($urls["user"])) $this->m_user = $urls["user"];
      if(!empty($urls["pass"])) $this->m_pass = $urls["pass"];
      if(!empty($urls["port"])) $this->m_port = $urls["port"];
      if(!empty($urls["path"])) $this->m_path = $urls["path"];
      $this->m_urlpath = $this->m_path;
      if(!empty($urls["query"])) {
        $this->m_query = $urls["query"];
        $this->m_urlpath .= "?".$this->m_query;
      }
    }
  }
  /**
  * 打开指定网址
  */
  function OpenUrl($url) {
    #重设各参数
    $this->m_url = "";
    $this->m_urlpath = "";
    $this->m_scheme = "http";
    $this->m_host = "";
    $this->m_port = "80";
    $this->m_user = "";
    $this->m_pass = "";
    $this->m_path = "/";
    $this->m_query = "";
    $this->m_error = "";
    $this->m_httphead = "" ;
    $this->m_html = "";
    $this->Close();
    #初始化系统
    $this->PrivateInit($url);
    $this->PrivateStartSession();
  }
  /**
  * 获得某操作错误的原因
  */
  public function printError() {
    echo "错误信息:".$this->m_error;
    echo "具体返回头:<br>";
    foreach($this->m_httphead as $k=>$v) {
      echo "$k => $v <br>\r\n";
    }
  }
  /**
  * 判别用Get方法发送的头的应答结果是否正确
  */
  public function IsGetOK() {
    if( ereg("^2",$this->GetHead("http-state")) ) {
      return true;
    } else {
      $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br>";
      return false;
    }
  }
  /**
  * 看看返回的网页是否是text类型
  */
  public function IsText() {
    if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {
      return true;
    } else {
      $this->m_error .= "内容为非文本类型<br>";
      return false;
    }
  }
  /**
  * 判断返回的网页是否是特定的类型
  */
  public function IsContentType($ctype) {
    if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {
      return true;
    } else {
      $this->m_error .= "类型不对 ".$this->GetHead("content-type")."<br>";
      return false;
    }
  }
  /**
  * 用 HTTP 协议下载文件
  */
  public function SaveToBin($savefilename) {
    if (!$this->IsGetOK()) return false;
    if (@feof($this->m_fp)) {
      $this->m_error = "连接已经关闭!";
      return false;
    }
    $fp = fopen($savefilename,"w") or die("写入文件 $savefilename 失败!");
    while (!feof($this->m_fp)) {
      @fwrite($fp,fgets($this->m_fp,256));
    }
    @fclose($this->m_fp);
    return true;
  }
  /**
  * 保存网页内容为 Text 文件
  */
  public function SaveToText($savefilename) {
    if ($this->IsText()) {
      $this->SaveBinFile($savefilename);
    } else {
      return "";
    }
  }
  /**
  * 用 HTTP 协议获得一个网页的内容
  */
  public function GetHtml() {
    if (!$this->IsText()) return "";
    if ($this->m_html!="") return $this->m_html;
    if (!$this->m_fp||@feof($this->m_fp)) return "";
    while(!feof($this->m_fp)) {
      $this->m_html .= fgets($this->m_fp,256);
    }
    @fclose($this->m_fp);
    return $this->m_html;
  }
  /**
  * 开始 HTTP 会话
  */
  public function PrivateStartSession() {
    if (!$this->PrivateOpenHost()) {
      $this->m_error .= "打开远程主机出错!";
      return false;
    }
    if ($this->GetHead("http-edition")=="HTTP/1.1") {
      $httpv = "HTTP/1.1";
    } else {
      $httpv = "HTTP/1.0";
    }
    fputs($this->m_fp,"GET ".$this->m_urlpath." $httpv\r\n");
    fputs($this->m_fp,"Host: ".$this->m_host."\r\n");
    fputs($this->m_fp,"Accept: */*\r\n");
    fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)\r\n");
    #HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
    if ($httpv=="HTTP/1.1") {
      fputs($this->m_fp,"Connection: Close\r\n\r\n");
    } else {
      fputs($this->m_fp,"\r\n");
    }
    $httpstas = fgets($this->m_fp,256);
    $httpstas = split(" ",$httpstas);
    $this->m_httphead["http-edition"] = trim($httpstas[0]);
    $this->m_httphead["http-state"] = trim($httpstas[1]);
    $this->m_httphead["http-describe"] = "";
    for ($i=2;$i<count($httpstas);$i++) {
      $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);
    }
    while (!feof($this->m_fp)) {
      $line = str_replace("\"","",trim(fgets($this->m_fp,256)));
      if($line == "") break;
      if (ereg(":",$line)) {
        $lines = split(":",$line);
        $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);
      }
    }
  }
  /**
  * 获得一个Http头的值
  */
  public function GetHead($headname) {
    $headname = strtolower($headname);
    if (isset($this->m_httphead[$headname])) {
      return $this->m_httphead[$headname];
    } else {
      return "";
    }
  }
  /**
  * 打开连接
  */
  public function PrivateOpenHost() {
    if ($this->m_host=="") return false;
    $this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10);
    if (!$this->m_fp){
      $this->m_error = $errstr;
      return false;
    } else {
      return true;
    }
  }
  /**
  * 关闭连接
  */
  public function Close(){
    @fclose($this->m_fp);
  }
}
#两种使用方法,分别如下:
#打开网页
$httpdown = new HttpDownload();
$httpdown->OpenUrl("http://www.google.com.hk");
echo $httpdown->GetHtml();
$httpdown->Close();
#下载文件
$file = new HttpDownload(); # 实例化类
$file->OpenUrl("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址
$file->SaveToBin("rust020.pdf"); # 保存路径及文件名
$file->Close(); # 释放资源
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
php 魔术方法使用说明
Oct 20 PHP
php检测图片木马多进制编程实践
Apr 11 PHP
PHP文件去掉PHP注释空格的函数分析(PHP代码压缩)
Jul 02 PHP
PHP获取当前所在目录位置的方法
Nov 26 PHP
php更新mysql后获取改变行数的方法
Dec 25 PHP
PHP Curl模拟登录微信公众平台、新浪微博实例代码
Jan 28 PHP
php使用函数pathinfo()、parse_url()和basename()解析URL
Nov 25 PHP
php curl上传、下载、https登陆实现代码
Jul 23 PHP
PHP中in_array的隐式转换的解决方法
Mar 06 PHP
PHP fprintf()函数用法讲解
Feb 16 PHP
PHP defined()函数的使用图文详解
Jul 20 PHP
PHP 数组操作详解【遍历、指针、函数等】
May 13 PHP
详解PHP使用Redis存储session时的一个Warning定位
Jul 05 #PHP
php如何修改SESSION的生存存储时间的实例代码
Jul 05 #PHP
PHP实现根据密码长度显示安全条
Jul 04 #PHP
PHP截取发动短信内容的方法
Jul 04 #PHP
phpcms配置列表页以及获得文章发布时间
Jul 04 #PHP
一个非常实用的php文件上传类
Jul 04 #PHP
php基于数组函数实现关联表的编辑操作示例
Jul 04 #PHP
You might like
用php+ajax新建流程(请假、进货、出货等)
2017/06/11 PHP
动态加载js的几种方法
2006/10/23 Javascript
js验证表单第二部分
2006/11/25 Javascript
js面向对象 多种创建对象方法小结
2012/05/21 Javascript
jquery判断浏览器类型的代码
2012/11/05 Javascript
关于jQuery中.attr()和.prop()的问题探讨
2013/09/06 Javascript
javascript 用函数语句和表达式定义函数的区别详解
2014/01/06 Javascript
jquery对单选框,多选框,文本框等常见操作小结
2014/01/08 Javascript
jquery ajax传递中文参数乱码问题及解决方法说明
2014/02/07 Javascript
JS控制一个DIV层在指定时间内消失的方法
2014/02/17 Javascript
EasyUI实现第二层弹出框的方法
2015/03/01 Javascript
javascript实现简单的省市区三级联动
2015/05/14 Javascript
jquery点击改变class并toggle的实现代码
2016/05/15 Javascript
AngularJS 表达式详细讲解及实例代码
2016/07/26 Javascript
JQuery实现DIV其他动画效果的简单实例
2016/09/18 Javascript
谈谈target=_new和_blank的不同之处
2016/10/25 Javascript
微信小程序 页面之间传参实例详解
2017/01/13 Javascript
使用html+js+css 实现页面轮播图效果(实例讲解)
2017/09/21 Javascript
基于Vue实现图书管理功能
2017/10/17 Javascript
webpack实现一个行内样式px转vw的loader示例
2018/09/13 Javascript
详解promise.then,process.nextTick, setTimeout 以及 setImmediate的执行顺序
2018/11/21 Javascript
Django rest framework基本介绍与代码示例
2018/01/26 Python
Python进阶之使用selenium爬取淘宝商品信息功能示例
2019/09/16 Python
解决Python发送Http请求时,中文乱码的问题
2020/04/30 Python
python代码区分大小写吗
2020/06/17 Python
中国网上药店领导者:1药网
2017/02/16 全球购物
好莱坞百老汇御用王牌美妆:Koh Gen Do 江原道
2018/04/03 全球购物
美国购买舞会礼服网站:Couture Candy
2019/12/29 全球购物
LINUX下线程,GDI类的解释
2016/12/14 面试题
物业电工岗位职责
2013/11/20 职场文书
秋季开学典礼主持词
2014/03/19 职场文书
求职信格式要求
2014/05/23 职场文书
小学毕业典礼演讲稿
2014/09/09 职场文书
处级领导干部四风问题自我剖析材料
2014/09/29 职场文书
聊聊pytorch测试的时候为何要加上model.eval()
2021/05/23 Python
python中urllib包的网络请求教程
2022/04/19 Python