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