|
...
|
...
|
@@ -76,53 +76,11 @@ class FileController |
|
|
|
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$size = $info['size'];
|
|
|
|
header("Content-Length: ".$size);
|
|
|
|
// 设置Content-Type头部
|
|
|
|
if($info['type'] == 'mp4'){
|
|
|
|
$header['Content-Type'] = 'video/' . $info['type'];
|
|
|
|
}
|
|
|
|
// 设置Accept-Ranges头部
|
|
|
|
$header['Accept-Ranges'] = 'bytes';
|
|
|
|
// 检查是否有范围请求
|
|
|
|
if (isset($_SERVER['HTTP_RANGE'])) {
|
|
|
|
$range = $_SERVER['HTTP_RANGE'];
|
|
|
|
$ranges = explode('-', substr($range, 6));
|
|
|
|
$start = intval($ranges[0]);
|
|
|
|
$end = $size - 1;
|
|
|
|
if (!empty($ranges[1])) {
|
|
|
|
$end = intval($ranges[1]);
|
|
|
|
}
|
|
|
|
$length = $end - $start + 1;
|
|
|
|
// 设置部分响应头部
|
|
|
|
$header['Content-Length'] = $length;
|
|
|
|
$header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
|
|
|
|
// 发送206 Partial Content状态码
|
|
|
|
header('HTTP/1.1 206 Partial Content');
|
|
|
|
header('Status: 206 Partial Content');
|
|
|
|
header('Accept-Ranges: bytes');
|
|
|
|
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
|
|
|
|
// 读取部分内容并发送响应
|
|
|
|
$file = fopen($path, 'rb');
|
|
|
|
fseek($file, $start);
|
|
|
|
$buffer = 1024 * 8; // 设置缓冲区大小
|
|
|
|
while (!feof($file) && ($p = ftell($file)) <= $end) {
|
|
|
|
if ($p + $buffer > $end) {
|
|
|
|
// 最后一块缓冲区
|
|
|
|
$buffer = $end - $p + 1;
|
|
|
|
}
|
|
|
|
echo fread($file, $buffer);
|
|
|
|
flush(); // 将输出刷新到浏览器
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
// 无范围请求,发送完整文件
|
|
|
|
$header['Content-Length'] = $size;
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
// 发送完整响应
|
|
|
|
foreach ($header as $name => $value) {
|
|
|
|
header("$name: $value");
|
|
|
|
}
|
|
|
|
echo $content;
|
|
|
|
header("Content-Type: video/mp4");
|
|
|
|
// 发送完整文件
|
|
|
|
readfile($path);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
...
|
...
|
|