php實現的支援斷點續傳的檔案下載類

來源:互聯網
上載者:User
本文執行個體講述了php實現的支援斷點續傳的檔案下載類及其用法,是非常實用的技巧。分享給大家供大家參考。具體方法如下:

通常來說,php支援斷點續傳,主要依靠HTTP協議中 header HTTP_RANGE實現。

HTTP斷點續傳原理:

Http頭 Range、Content-Range()
HTTP頭中一般斷點下載時才用到Range和Content-Range實體頭,
Range使用者要求標頭中,指定第一個位元組的位置和最後一個位元組的位置,如(Range:200-300)
Content-Range用於回應標頭

請求下載整個檔案:

GET /test.rar HTTP/1.1
Connection: close
Host: 116.1.219.219
Range: bytes=0-801 //一般請求下載整個檔案是bytes=0- 或不用這個頭

一般正常回應:

HTTP/1.1 200 OK
Content-Length: 801
Content-Type: application/octet-stream
Content-Range: bytes 0-800/801 //801:檔案總大小

FileDownload.class.php類檔案代碼如下:


getRange($file_size);         header('cache-control:public');       header('content-type:application/octet-stream');       header('content-disposition:attachment; filename='.$name);         if($reload && $ranges!=null){ // 使用續傳         header('HTTP/1.1 206 Partial Content');         header('Accept-Ranges:bytes');                   // 剩餘長度         header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));                   // range資訊         header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));                   // fp指標跳到斷點位置         fseek($fp, sprintf('%u', $ranges['start']));       }else{         header('HTTP/1.1 200 OK');         header('content-length:'.$file_size);       }         while(!feof($fp)){         echo fread($fp, round($this->_speed*1024,0));         ob_flush();         //sleep(1); // 用於測試,減慢下載速度       }         ($fp!=null) && fclose($fp);       }else{       return '';     }   }     /** 設定下載速度   * @param int $speed   */  public function setSpeed($speed){     if(is_numeric($speed) && $speed>16 && $speed<4096){       $this->_speed = $speed;     }   }     /** 擷取header range資訊   * @param int  $file_size 檔案大小   * @return Array   */  private function getRange($file_size){     if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){       $range = $_SERVER['HTTP_RANGE'];       $range = preg_replace('/[\s|,].*/', '', $range);       $range = explode('-', substr($range, 6));       if(count($range)<2){         $range[1] = $file_size;       }       $range = array_combine(array('start','end'), $range);       if(empty($range['start'])){         $range['start'] = 0;       }       if(empty($range['end'])){         $range['end'] = $file_size;       }       return $range;     }     return null;   } } // class end   ?> 

demo範例程式碼如下:

download($file, $name); //$flag = $obj->download($file, $name, true); // 斷點續傳   if(!$flag){   echo 'file not exists'; } ?>

斷點續傳測試方法:

使用linux wget命令去測試下載, wget -c -O file http://xxx

1.先關閉斷點續傳

$flag = $obj->download($file, $name);

test@ubuntu:~/Downloads$ wget -O test.rar http://demo.test.com/demo.php --2013-06-30 16:52:44-- http://demo.test.com/demo.php 正在解析主機 demo.test.com... 127.0.0.1 正在串連 demo.test.com|127.0.0.1|:80... 已串連。 已發出 HTTP 要求,正在等待回應... 200 OK 長度: 10445120 (10.0M) [application/octet-stream] 正在儲存至: “test.rar”   30% [============================>                                   ] 3,146,580  513K/s 估時 14s ^C test@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.test.com/demo.php --2013-06-30 16:52:57-- http://demo.test.com/demo.php 正在解析主機 demo.test.com... 127.0.0.1 正在串連 demo.test.com|127.0.0.1|:80... 已串連。 已發出 HTTP 要求,正在等待回應... 200 OK 長度: 10445120 (10.0M) [application/octet-stream] 正在儲存至: “test.rar” 30% [============================>                                   ] 3,146,580  515K/s 估時 14s ^C 

可以看到,wget -c不能斷點續傳

2.開啟斷點續傳

$flag = $obj->download($file, $name, true);

test@ubuntu:~/Downloads$ wget -O test.rar http://demo.test.com/demo.php --2013-06-30 16:53:19-- http://demo.test.com/demo.php 正在解析主機 demo.test.com... 127.0.0.1 正在串連 demo.test.com|127.0.0.1|:80... 已串連。 已發出 HTTP 要求,正在等待回應... 200 OK 長度: 10445120 (10.0M) [application/octet-stream] 正在儲存至: “test.rar”   20% [==================>                                        ] 2,097,720  516K/s 估時 16s ^C test@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.test.com/demo.php --2013-06-30 16:53:31-- http://demo.test.com/demo.php 正在解析主機 demo.test.com... 127.0.0.1 正在串連 demo.test.com|127.0.0.1|:80... 已串連。 已發出 HTTP 要求,正在等待回應... 206 Partial Content 長度: 10445121 (10.0M),7822971 (7.5M) 位元組剩餘 [application/octet-stream] 正在儲存至: “test.rar”   100%[++++++++++++++++++++++++=========================================================================>] 10,445,121  543K/s  花時 14s     2013-06-30 16:53:45 (543 KB/s) - 已儲存 “test.rar” [10445121/10445121]) 

可以看到會從斷點的位置(%20)開始下載。


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.