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);

 

posted on 2019-09-03 11:25  风暴烈酒辰 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Shadow3627/p/11451962.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值