作者 lyh

gx

@@ -88,38 +88,38 @@ class FileController @@ -88,38 +88,38 @@ class FileController
88 // 设置Accept-Ranges头部 88 // 设置Accept-Ranges头部
89 $header['Accept-Ranges'] = 'bytes'; 89 $header['Accept-Ranges'] = 'bytes';
90 // 检查是否有范围请求 90 // 检查是否有范围请求
91 -// if (isset($_SERVER['HTTP_RANGE'])) {  
92 -// $range = $_SERVER['HTTP_RANGE'];  
93 -// $ranges = explode('-', substr($range, 6));  
94 -// $start = intval($ranges[0]);  
95 -// $end = $size - 1;  
96 -// if (!empty($ranges[1])) {  
97 -// $end = intval($ranges[1]);  
98 -// }  
99 -// $length = $end - $start + 1;  
100 -// // 设置部分响应头部  
101 -// $header['Content-Length'] = $length;  
102 -// $header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size;  
103 -// // 发送206 Partial Content状态码  
104 -// header('HTTP/1.1 206 Partial Content');  
105 -// header('Status: 206 Partial Content');  
106 -// header('Accept-Ranges: bytes');  
107 -// header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);  
108 -// // 读取部分内容并发送响应  
109 -// $file = fopen($path, 'rb');  
110 -// fseek($file, $start);  
111 -// $buffer = 1024 * 8; // 设置缓冲区大小  
112 -// while (!feof($file) && ($p = ftell($file)) <= $end) {  
113 -// if ($p + $buffer > $end) {  
114 -// // 最后一块缓冲区  
115 -// $buffer = $end - $p + 1;  
116 -// }  
117 -// echo fread($file, $buffer);  
118 -// flush(); // 将输出刷新到浏览器  
119 -// }  
120 -// fclose($file);  
121 -// exit;  
122 -// } 91 + if (isset($_SERVER['HTTP_RANGE'])) {
  92 + $range = $_SERVER['HTTP_RANGE'];
  93 + $ranges = explode('-', substr($range, 6));
  94 + $start = intval($ranges[0]);
  95 + $end = $size - 1;
  96 + if (!empty($ranges[1])) {
  97 + $end = intval($ranges[1]);
  98 + }
  99 + $length = $end - $start + 1;
  100 + // 设置部分响应头部
  101 + $header['Content-Length'] = $length;
  102 + $header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
  103 + // 发送206 Partial Content状态码
  104 + header('HTTP/1.1 206 Partial Content');
  105 + header('Status: 206 Partial Content');
  106 + header('Accept-Ranges: bytes');
  107 + header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
  108 + // 读取部分内容并发送响应
  109 + $file = fopen($path, 'rb');
  110 + fseek($file, $start);
  111 + $buffer = 1024 * 8; // 设置缓冲区大小
  112 + while (!feof($file) && ($p = ftell($file)) <= $end) {
  113 + if ($p + $buffer > $end) {
  114 + // 最后一块缓冲区
  115 + $buffer = $end - $p + 1;
  116 + }
  117 + echo fread($file, $buffer);
  118 + flush(); // 将输出刷新到浏览器
  119 + }
  120 + fclose($file);
  121 + exit;
  122 + }
123 // 无范围请求,发送完整文件 123 // 无范围请求,发送完整文件
124 $header['Content-Length'] = $size; 124 $header['Content-Length'] = $size;
125 $content = file_get_contents($path); 125 $content = file_get_contents($path);