實現的支援斷點續傳的檔案下載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類檔案代碼如下:

  1. /**
  2. * 下載類,支援斷點續傳
  3. * @time : 2015 06 30 09:36
  4. * @author:guoyu@xzdkiosk.com
  5. * php下載類,支援斷點續傳
  6. * Func:
  7. * download: 下載檔案
  8. * setSpeed: 設定下載速度
  9. * getRange: 擷取header中Range
  10. */
  11. class FileDownload{ // class start
  12. private $_speed = 512; // 下載速度
  13. /** 下載
  14. * @param String $file 要下載的檔案路徑
  15. * @param String $name 檔案名稱,為空白則與下載的檔案名稱一樣
  16. * @param boolean $reload 是否開啟斷點續傳
  17. */
  18. public function download($file, $name='', $reload=false){
  19. if(file_exists($file)){
  20. if($name==''){
  21. $name = basename($file);
  22. }
  23. $fp = fopen($file, 'rb');
  24. $file_size = filesize($file);
  25. $ranges = $this->getRange($file_size);
  26. header('cache-control:public');
  27. header('content-type:application/octet-stream');
  28. header('content-disposition:attachment; filename='.$name);
  29. if($reload && $ranges!=null){ // 使用續傳
  30. header('HTTP/1.1 206 Partial Content');
  31. header('Accept-Ranges:bytes');
  32. // 剩餘長度
  33. header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));
  34. // range資訊
  35. header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));
  36. // fp指標跳到斷點位置
  37. fseek($fp, sprintf('%u', $ranges['start']));
  38. }else{
  39. header('HTTP/1.1 200 OK');
  40. header('content-length:'.$file_size);
  41. }
  42. while(!feof($fp)){
  43. echo fread($fp, round($this->_speed*1024,0));
  44. ob_flush();
  45. //sleep(1); // 用於測試,減慢下載速度
  46. }
  47. ($fp!=null) && fclose($fp);
  48. }else{
  49. return '';
  50. }
  51. }
  52. /** 設定下載速度
  53. * @param int $speed
  54. */
  55. public function setSpeed($speed){
  56. if(is_numeric($speed) && $speed>16 && $speed<4096){
  57. $this->_speed = $speed;
  58. }
  59. }
  60. /** 擷取header range資訊
  61. * @param int $file_size 檔案大小
  62. * @return Array
  63. */
  64. private function getRange($file_size){
  65. if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
  66. $range = $_SERVER['HTTP_RANGE'];
  67. $range = preg_replace('/[\s|,].*/', '', $range);
  68. $range = explode('-', substr($range, 6));
  69. if(count($range)<2){
  70. $range[1] = $file_size;
  71. }
  72. $range = array_combine(array('start','end'), $range);
  73. if(empty($range['start'])){
  74. $range['start'] = 0;
  75. }
  76. if(empty($range['end'])){
  77. $range['end'] = $file_size;
  78. }
  79. return $range;
  80. }
  81. return null;
  82. }
  83. } // class end
  84. ?>
複製代碼
demo範例程式碼如下:
  1. require('FileDownload.class.php');
  2. $file = 'book.zip';
  3. $name = time().'.zip';
  4. $obj = new FileDownload();
  5. $flag = $obj->download($file, $name);
  6. //$flag = $obj->download($file, $name, true); // 斷點續傳
  7. if(!$flag){
  8. echo 'file not exists';
  9. }
  10. ?>
複製代碼

斷點續傳測試方法:

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

1.先關閉斷點續傳

  1. $flag = $obj->download($file, $name);
複製代碼
  1. test@ubuntu:~/Downloads$ wget -O test.rar http://demo.test.com/demo.php
  2. --2013-06-30 16:52:44-- http://demo.test.com/demo.php
  3. 正在解析主機 demo.test.com... 127.0.0.1
  4. 正在串連 demo.test.com|127.0.0.1|:80... 已串連。
  5. 已發出 HTTP 要求,正在等待回應... 200 OK
  6. 長度: 10445120 (10.0M) [application/octet-stream]
  7. 正在儲存至: “test.rar”
  8. 30% [============================> ] 3,146,580 513K/s 估時 14s
  9. ^C
  10. test@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.test.com/demo.php
  11. --2013-06-30 16:52:57-- http://demo.test.com/demo.php
  12. 正在解析主機 demo.test.com... 127.0.0.1
  13. 正在串連 demo.test.com|127.0.0.1|:80... 已串連。
  14. 已發出 HTTP 要求,正在等待回應... 200 OK
  15. 長度: 10445120 (10.0M) [application/octet-stream]
  16. 正在儲存至: “test.rar”
  17. 30% [============================> ] 3,146,580 515K/s 估時 14s
  18. ^C
複製代碼

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

2.開啟斷點續傳

  1. $flag = $obj->download($file, $name, true);
複製代碼
  1. test@ubuntu:~/Downloads$ wget -O test.rar http://demo.test.com/demo.php
  2. --2013-06-30 16:53:19-- http://demo.test.com/demo.php
  3. 正在解析主機 demo.test.com... 127.0.0.1
  4. 正在串連 demo.test.com|127.0.0.1|:80... 已串連。
  5. 已發出 HTTP 要求,正在等待回應... 200 OK
  6. 長度: 10445120 (10.0M) [application/octet-stream]
  7. 正在儲存至: “test.rar”
  8. 20% [==================> ] 2,097,720 516K/s 估時 16s
  9. ^C
  10. test@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.test.com/demo.php
  11. --2013-06-30 16:53:31-- http://demo.test.com/demo.php
  12. 正在解析主機 demo.test.com... 127.0.0.1
  13. 正在串連 demo.test.com|127.0.0.1|:80... 已串連。
  14. 已發出 HTTP 要求,正在等待回應... 206 Partial Content
  15. 長度: 10445121 (10.0M),7822971 (7.5M) 位元組剩餘 [application/octet-stream]
  16. 正在儲存至: “test.rar”
  17. 100%[++++++++++++++++++++++++=========================================================================>] 10,445,121 543K/s 花時 14s
  18. 2013-06-30 16:53:45 (543 KB/s) - 已儲存 “test.rar” [10445121/10445121])
複製代碼
可以看到會從斷點的位置(%20)開始下載。

來自:http://blog.csdn.net/phpfenghuo/article/details/46691865
斷點續傳, php
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.