• php的一个断点续传下载实现


     1         $file = "我是一个路径";
     2         if(empty($file)){
     3             $this->displayJson(Ap_Constants::ERROR_EMPTY);
     4         }
     5 
     6         
     7         $file_name = $file['name'];
     8         $size = $file['size'];
     9         //中文需要转码
    10         $path = iconv('UTF-8', 'GB2312', $file['path']);
    11         $size = $filesize=filesize($path);
    12 
    13    
    14         $fm = @fopen ( $path, 'rb' );
    15         $begin = 0;
    16         $end = $size - 1;
    17          
    18         if (isset ( $_SERVER ['HTTP_RANGE'] )) {
    19             if (preg_match ( '/bytes=h*(d+)-(d*)[D.*]?/i', $_SERVER ['HTTP_RANGE'], $matches )) {
    20                 // 读取文件,起始节点
    21                 $begin = intval ( $matches [1] );
    22          
    23                 // 读取文件,结束节点
    24                 if (! empty ( $matches [2] )) {
    25                     $end = intval ( $matches [2] );
    26                 }
    27             }
    28         }
    29          
    30         if (isset ( $_SERVER ['HTTP_RANGE'] )) {
    31             header ( 'HTTP/1.1 206 Partial Content' );
    32         } else {
    33             header ( 'HTTP/1.1 200 OK' );
    34         }
    35          
    36         Header("Content-type: application/vnd.android.package-archive");
    37         header ( 'Cache-Control: public, must-revalidate, max-age=0' );
    38         header ( 'Pragma: no-cache' );
    39         header ( 'Accept-Ranges: bytes' );
    40         header ( 'Content-Length:' . (($end - $begin) + 1) );
    41          
    42         if (isset ( $_SERVER ['HTTP_RANGE'] )) {
    43             header ( "Content-Range: bytes $begin-$end/$size" );
    44         }
    45          
    46         header ( "Content-Disposition: inline; filename=$file_name" );
    47         header ( "Content-Transfer-Encoding: binary" );
    48          
    49         $cur = $begin;
    50          
    51         // 定位指针
    52         fseek ( $fm, $begin, 0 );
    53         ob_clean();
    54         flush();
    55         while ( ! feof ( $fm ) && $cur <= $end && (connection_status () == 0) ) {
    56             print fread ( $fm, min ( 1024 * 16, ($end - $cur) + 1 ) );
    57             $cur += 1024 * 16;
    58         }
    59         exit(0);
  • 相关阅读:
    计算机组成原理——辅助存储器
    什么是区块链?
    博客园添加背景音乐插件
    计算机组成原理——《深入理解计算机系统》|虚拟存储器
    计算机组成原理——主存储器考研题
    C++ 构造函数初始化列表
    C++ 运行时类别识别
    华为,加油!
    计算机组成原理——cache高速缓存存储器
    计算机组成原理——按字节编址与按字编址
  • 原文地址:https://www.cnblogs.com/Shadow3627/p/11451962.html
Copyright © 2020-2023  润新知