php download断点

FileDownload.class.php

    1.     <?php    
    2.     /** php下载类,支持断点续传  
    3.     *   Date:   2013-06-30  
    4.     *   Author: fdipzone  
    5.     *   Ver:    1.0  
    6.     *  
    7.     *   Func:  
    8.     *   download: 下载文件  
    9.     *   setSpeed: 设置下载速度  
    10.     *   getRange: 获取header中Range  
    11.     */    
    12.         
    13.     class FileDownload{ // class start    
    14.         
    15.         private $_speed = 512;   // 下载速度    
    16.         
    17.         
    18.         /** 下载  
    19.         * @param String  $file   要下载的文件路径  
    20.         * @param String  $name   文件名称,为空则与下载的文件名称一样  
    21.         * @param boolean $reload 是否开启断点续传  
    22.         */    
    23.         public function download($file, $name='', $reload=false){    
    24.             if(file_exists($file)){    
    25.                 if($name==''){    
    26.                     $name = basename($file);    
    27.                 }    
    28.         
    29.                 $fp = fopen($file, 'rb');    
    30.                 $file_size = filesize($file);    
    31.                 $ranges = $this->getRange($file_size);    
    32.         
    33.                 header('cache-control:public');    
    34.                 header('content-type:application/octet-stream');    
    35.                 header('content-disposition:attachment; filename='.$name);    
    36.         
    37.                 if($reload && $ranges!=null){ // 使用续传    
    38.                     header('HTTP/1.1 206 Partial Content');    
    39.                     header('Accept-Ranges:bytes');    
    40.                         
    41.                     // 剩余长度    
    42.                     header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));    
    43.                         
    44.                     // range信息    
    45.                     header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));    
    46.                         
    47.                     // fp指针跳到断点位置    
    48.                     fseek($fp, sprintf('%u', $ranges['start']));    
    49.                 }else{    
    50.                     header('HTTP/1.1 200 OK');    
    51.                     header('content-length:'.$file_size);    
    52.                 }    
    53.         
    54.                 while(!feof($fp)){    
    55.                     echo fread($fp, round($this->_speed*1024,0));    
    56.                     ob_flush();    
    57.                     //sleep(1); // 用于测试,减慢下载速度    
    58.                 }    
    59.         
    60.                 ($fp!=null) && fclose($fp);    
    61.         
    62.             }else{    
    63.                 return '';    
    64.             }    
    65.         }    
    66.         
    67.         
    68.         /** 设置下载速度  
    69.         * @param int $speed  
    70.         */    
    71.         public function setSpeed($speed){    
    72.             if(is_numeric($speed) && $speed>16 && $speed<4096){    
    73.                 $this->_speed = $speed;    
    74.             }    
    75.         }    
    76.         
    77.         
    78.         /** 获取header range信息  
    79.         * @param  int   $file_size 文件大小  
    80.         * @return Array  
    81.         */    
    82.         private function getRange($file_size){    
    83.             if(isset($_SERVER['HTTP_RANGE']) && !emptyempty($_SERVER['HTTP_RANGE'])){    
    84.                 $range = $_SERVER['HTTP_RANGE'];    
    85.                 $range = preg_replace('/[s|,].*/', '', $range);    
    86.                 $range = explode('-', substr($range, 6));    
    87.                 if(count($range)<2){    
    88.                     $range[1] = $file_size;    
    89.                 }    
    90.                 $range = array_combine(array('start','end'), $range);    
    91.                 if(emptyempty($range['start'])){    
    92.                     $range['start'] = 0;    
    93.                 }    
    94.                 if(emptyempty($range['end'])){    
    95.                     $range['end'] = $file_size;    
    96.                 }    
    97.                 return $range;    
    98.             }    
    99.             return null;    
    100.         }    
    101.         
    102.     } // class end    
    103.         
    104.     ?>    
    105.   
    106. demo  
    107. [codes=php]  
    108.   
    109.     <?php    
    110.         
    111.     require('FileDownload.class.php');    
    112.     $file = 'book.zip';    
    113.     $name = time().'.zip';    
    114.     $obj = new FileDownload();    
    115.     $flag = $obj->download($file, $name);    
    116.     //$flag = $obj->download($file, $name, true); // 断点续传    
    117.         
    118.     if(!$flag){    
    119.         echo 'file not exists';    
    120.     }    
    121.         
    122.     ?>    
    123.   
    124. 断点续传测试方法:  
    125. 使用linux wget命令去测试下载, wget -c -O file http://xxx  
    126.   
    127. 1.先关闭断点续传  
    128. $flag = $obj->download($file, $name);  
    129. [plain] view plaincopy  
    130.   
    131.     fdipzone@ubuntu:~/Downloads$ wget -O test.rar http://demo.fdipzone.com/demo.php    
    132.     --2013-06-30 16:52:44--  http://demo.fdipzone.com/demo.php    
    133.     正在解析主机 demo.fdipzone.com... 127.0.0.1    
    134.     正在连接 demo.fdipzone.com|127.0.0.1|:80... 已连接。    
    135.     已发出 HTTP 请求,正在等待回应... 200 OK    
    136.     长度: 10445120 (10.0M) [application/octet-stream]    
    137.     正在保存至: “test.rar”    
    138.         
    139.     30% [============================>                                                                     ] 3,146,580    513K/s  估时 14s    
    140.     ^C    
    141.     fdipzone@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.fdipzone.com/demo.php    
    142.     --2013-06-30 16:52:57--  http://demo.fdipzone.com/demo.php    
    143.     正在解析主机 demo.fdipzone.com... 127.0.0.1    
    144.     正在连接 demo.fdipzone.com|127.0.0.1|:80... 已连接。    
    145.     已发出 HTTP 请求,正在等待回应... 200 OK    
    146.     长度: 10445120 (10.0M) [application/octet-stream]    
    147.     正在保存至: “test.rar”    
    148.         
    149.     30% [============================>                                                                     ] 3,146,580    515K/s  估时 14s    
    150.     ^C    
    151.         
    152.     可以看到,wget -c不能断点续传    
    153.   
    154.   
    155. 2.开启断点续传  
    156. $flag = $obj->download($file, $name, true);  
    157. [plain] view plaincopy  
    158.   
    159.     fdipzone@ubuntu:~/Downloads$ wget -O test.rar http://demo.fdipzone.com/demo.php    
    160.     --2013-06-30 16:53:19--  http://demo.fdipzone.com/demo.php    
    161.     正在解析主机 demo.fdipzone.com... 127.0.0.1    
    162.     正在连接 demo.fdipzone.com|127.0.0.1|:80... 已连接。    
    163.     已发出 HTTP 请求,正在等待回应... 200 OK    
    164.     长度: 10445120 (10.0M) [application/octet-stream]    
    165.     正在保存至: “test.rar”    
    166.         
    167.     20% [==================>                                                                               ] 2,097,720    516K/s  估时 16s    
    168.     ^C    
    169.     fdipzone@ubuntu:~/Downloads$ wget -c -O test.rar http://demo.fdipzone.com/demo.php    
    170.     --2013-06-30 16:53:31--  http://demo.fdipzone.com/demo.php    
    171.     正在解析主机 demo.fdipzone.com... 127.0.0.1    
    172.     正在连接 demo.fdipzone.com|127.0.0.1|:80... 已连接。    
    173.     已发出 HTTP 请求,正在等待回应... 206 Partial Content    
    174.     长度: 10445121 (10.0M),7822971 (7.5M) 字节剩余 [application/octet-stream]    
    175.     正在保存至: “test.rar”    
    176.         
    177.     100%[++++++++++++++++++++++++=========================================================================>] 10,445,121   543K/s   花时 14s       
    178.         
    179.     2013-06-30 16:53:45 (543 KB/s) - 已保存 “test.rar” [10445121/10445121])    
    180.         
    181.     可以看到会从断点的位置(%20)开始下载。    
    182.   
    183. 源码下载地址:<a href="http://download.csdn.net/detail/fdipzone/5676439" target="_blank">点击下载 </a>  
    184. 摘自:http://blog.csdn.net/fdipzone/article/details/9208221  
    185.   
    186.   
    187. PHP上传实现断点续传文件的方法:  
    188. 其实说简单点就是通过这个变量$_SERVER['HTTP_RANGE']取得用户请求的文件的range,然后程序去控制文件的输出。比如第一次请求一个文件的从0到999字节,第二次请求1000到1999字节,以此类推,每次请求1000字节的内容,然后程序通过fseek函数去取得对应的文件位置,然后输出。  
    189. [codes=php]  
    190.     $fname = './05e58c19552bb26b158f6621a6650899';   
    191.     $fp = fopen($fname,'rb');   
    192.     $fsize = filesize($fname);   
    193.     if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-$/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1] < $fsize)) {   
    194.     $start = $match[1];   
    195.     } else {   
    196.     $start = 0;   
    197.     }   
    198.     @header("Cache-control: public");   
    199.     @header("Pragma: public");   
    200.     if ($start > 0) {   
    201.     fseek($fp, $start);   
    202.     Header("HTTP/1.1 206 Partial Content");   
    203.     Header("Content-Length: " . ($fsize - $start));   
    204.     Header("Content-Ranges: bytes" . $start . "-" . ($fsize - 1) . "/" . $fsize);   
    205.     } else {   
    206.     header("Content-Length: $fsize");   
    207.     Header("Accept-Ranges: bytes");   
    208.     }   
    209.     @header("Content-Type: application/octet-stream");   
    210.     @header("Content-Disposition: attachment;filename=1.rm");   
    211.     fpassthru($fp);  
原文地址:https://www.cnblogs.com/gpfeisoft/p/4941054.html