正在显示
1 个修改的文件
包含
46 行增加
和
4 行删除
| @@ -76,11 +76,53 @@ class FileController | @@ -76,11 +76,53 @@ class FileController | ||
| 76 | $this->response('指定文件已被系统删除!', Code::USER_ERROR); | 76 | $this->response('指定文件已被系统删除!', Code::USER_ERROR); |
| 77 | } | 77 | } |
| 78 | $size = $info['size']; | 78 | $size = $info['size']; |
| 79 | - header("Content-Length: ".$size); | ||
| 80 | // 设置Content-Type头部 | 79 | // 设置Content-Type头部 |
| 81 | - header("Content-Type: video/mp4"); | ||
| 82 | - // 发送完整文件 | ||
| 83 | - readfile($path); | 80 | + if($info['type'] == 'mp4'){ |
| 81 | + $header['Content-Type'] = 'video/' . $info['type']; | ||
| 82 | + } | ||
| 83 | + // 设置Accept-Ranges头部 | ||
| 84 | + $header['Accept-Ranges'] = 'bytes'; | ||
| 85 | + // 检查是否有范围请求 | ||
| 86 | + if (isset($_SERVER['HTTP_RANGE'])) { | ||
| 87 | + $range = $_SERVER['HTTP_RANGE']; | ||
| 88 | + $ranges = explode('-', substr($range, 6)); | ||
| 89 | + $start = intval($ranges[0]); | ||
| 90 | + $end = $size - 1; | ||
| 91 | + if (!empty($ranges[1])) { | ||
| 92 | + $end = intval($ranges[1]); | ||
| 93 | + } | ||
| 94 | + $length = $end - $start + 1; | ||
| 95 | + // 设置部分响应头部 | ||
| 96 | + $header['Content-Length'] = $length; | ||
| 97 | + $header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size; | ||
| 98 | + // 发送206 Partial Content状态码 | ||
| 99 | + header('HTTP/1.1 206 Partial Content'); | ||
| 100 | + header('Status: 206 Partial Content'); | ||
| 101 | + header('Accept-Ranges: bytes'); | ||
| 102 | + header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); | ||
| 103 | + // 读取部分内容并发送响应 | ||
| 104 | + $file = fopen($path, 'rb'); | ||
| 105 | + fseek($file, $start); | ||
| 106 | + $buffer = 1024 * 8; // 设置缓冲区大小 | ||
| 107 | + while (!feof($file) && ($p = ftell($file)) <= $end) { | ||
| 108 | + if ($p + $buffer > $end) { | ||
| 109 | + // 最后一块缓冲区 | ||
| 110 | + $buffer = $end - $p + 1; | ||
| 111 | + } | ||
| 112 | + echo fread($file, $buffer); | ||
| 113 | + flush(); // 将输出刷新到浏览器 | ||
| 114 | + } | ||
| 115 | + fclose($file); | ||
| 116 | + exit; | ||
| 117 | + } | ||
| 118 | + // 无范围请求,发送完整文件 | ||
| 119 | + $header['Content-Length'] = $size; | ||
| 120 | + $content = file_get_contents($path); | ||
| 121 | + // 发送完整响应 | ||
| 122 | + foreach ($header as $name => $value) { | ||
| 123 | + header("$name: $value"); | ||
| 124 | + } | ||
| 125 | + echo $content; | ||
| 84 | exit; | 126 | exit; |
| 85 | } | 127 | } |
| 86 | 128 |
-
请 注册 或 登录 后发表评论