$size = filesize($localfile);
header("Accept-Ranges: 0-$size");
header("Content-Type: video/mp4");
if (isset($_SERVER['HTTP_RANGE'])) {
if (!preg_match('/^bytes=\d*-\d*(,\d*-\d*)*$/i', $_SERVER['HTTP_RANGE'])) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
$ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
foreach ($ranges as $range) {
$parts = explode('-', $range);
$ranges_arr[] = array($parts[0],$parts[1]);
$ranges = $ranges_arr[0];
$length = (int)$ranges[1];
$start = $size - $length;
header('HTTP/1.1 416 Requested Range Not Satisfiable');
$start = (int)$ranges[0];
$length = $end - $start + 1;
header('HTTP/1.1 206 PARTIAL CONTENT');
header("Content-Range: bytes {$start}-{$end}/{$size}");
header("Content-Length: $length");
$file = fopen($localfile, 'rb');
while (!feof($file) && ($p = ftell($file)) <= $end){
if ($p + $buffer > $end) {
echo fread($file, $buffer);