Heim  >  Artikel  >  Backend-Entwicklung  >  实现的支持断点续传的文件下载php类

实现的支持断点续传的文件下载php类

WBOY
WBOYOriginal
2016-07-25 08:42:491799Durchsuche

本文实例讲述了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 $this->_speed = $speed;
  57. }
  58. }
  59. /** 获取header range信息
  60. * @param int $file_size 文件大小
  61. * @return Array
  62. */
  63. private function getRange($file_size){
  64. if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
  65. $range = $_SERVER['HTTP_RANGE'];
  66. $range = preg_replace('/[\s|,].*/', '', $range);
  67. $range = explode('-', substr($range, 6));
  68. if(count($range) $range[1] = $file_size;
  69. }
  70. $range = array_combine(array('start','end'), $range);
  71. if(empty($range['start'])){
  72. $range['start'] = 0;
  73. }
  74. if(empty($range['end'])){
  75. $range['end'] = $file_size;
  76. }
  77. return $range;
  78. }
  79. return null;
  80. }
  81. } // class end
  82. ?>
复制代码

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


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn