PHP下載遠程文件類(支持斷點續傳)

1.功能:支持斷點續傳的下載,能計算傳輸率,能控制傳輸率


簡易使用方法:


$object = new httpdownload();
$object->set_byfile($file)%N#H#%;//服務器文件名,包括路徑
$object->filename = $filename;//下載另存為的文件名
$object->download();


3.源文件:


<?
      class httpdownload {
      var $data = null;
      var $data_len = 0;
      var $data_mod = 0;
      var $data_type = 0;
      var $data_section = 0; //section download
      var $sentSize=0;
      var $handler = array(auth => null);
      var $use_resume = true;
      var $use_autoexit = false;
      var $use_auth = false;
      var $filename = null;
      var $mime = null;
      var $bufsize = 2048;
      var $seek_start = 0;
      var $seek_end = -1;
      var $totalsizeref = 0;
      var $bandwidth = 0;
      var $speed = 0;
      function initialize() {
            global $HTTP_SERVER_VARS;
            if ($this->use_auth) //use authentication {
                  if (!$this->_auth()) //no authentication {
                          header(WWW-Authenticate: Basic realm=”Please enter your username and password”);
                          header(HTTP/1.0 401 Unauthorized);
                          header(status: 401 Unauthorized);
                          if ($this->use_autoexit) exit();
                          return false;
                  }
            }
            if ($this->mime == null) $this->mime = “application/octet-stream”; //default mime
            if (isset($_SERVER[HTTP_RANGE]) || isset($HTTP_SERVER_VARS[HTTP_RANGE])) {
                  if (isset($HTTP_SERVER_VARS[HTTP_RANGE])) $seek_range = substr($HTTP_SERVER_VARS[HTTP_RANGE] , strlen(bytes=));
                  else $seek_range = substr($_SERVER[HTTP_RANGE] , strlen(bytes=));
            $range = explode(-,$seek_range);
            if ($range[0] > 0) {
                  $this->seek_start = intval($range[0]);
            }
            if ($range[1] > 0) $this->seek_end = intval($range[1]);
            else $this->seek_end = -1;
            if (!$this->use_resume) {
                  $this->seek_start = 0;
                  //header(“HTTP/1.0 404 Bad Request”);
                  //header(“Status: 400 Bad Request”);
                  //exit;
                  //return false;
            } else {
                  $this->data_section = 1;
            }
      } else {
            $this->seek_start = 0;
            $this->seek_end = -1;
      }
      $this->sentSize=0;
      return true;
}
function header($size,$seek_start=null,$seek_end=null) {
      header(Content-type: . $this->mime);
      header(Content-Disposition: attachment; filename=” . $this->filename . “);
      header(Last-Modified: . date(D, d M Y H:i:s GMT , $this->data_mod));
      if ($this->data_section && $this->use_resume) {
            header(“HTTP/1.0 206 Partial Content”);
            header(“Status: 206 Partial Content”);
            header(Accept-Ranges: bytes);
            header(“Content-Range: bytes $seek_start-$seek_end/$size”);
            header(“Content-Length: ” . ($seek_end – $seek_start + 1));
      } else {
            header(“Content-Length: $size”);
      }
}
function download_ex($size) {
      if (!$this->initialize()) return false;
      ignore_user_abort(true);
      //Use seek end here
      if ($this->seek_start > ($size – 1)) $this->seek_start = 0;
      if ($this->seek_end <= 0) $this->seek_end = $size – 1;
      $this->header($size,$seek,$this->seek_end);
      $this->data_mod = time();
      return true;
}
function download() {
      if (!$this->initialize()) return false;
      try {
            error_log(“begin download
“, 3,”/usr/local/www/apache22/LOGS/apache22_php.err”);
    &nb

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *