$fp = fopen($file, 'r');
$size = filesize($file);
$begin = 0;
$end = $size - 1;
if (isset($_SERVER['HTTP_RANGE'])) {
header('HTTP/1.1 206 Partial Content');
if (preg_match('/bytes=\b(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
$begin = intval($matches[1]);
if (!empty($matches[2])) {
$end = intval($matches[2]);
}
}
header('Content-Range: bytes ' . $begin . '-' . $end . '/' . $size);
} else {
header('HTTP/1.1 200 OK');
}
header('Cache-control: public');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Type: video/mp4');
header('Content-Transfer-Encoding: binary');
header('Content-Length:' . (($end - $begin) + 1));
fseek($fp, $begin);
$length = 0;
while (!feof($fp) && $length <= $end - $begin + 1) {
echo fread($fp, 8192);
ob_flush();
flush();
$length += 8192;
}
fclose($fp);
|