'HTTP Range requests with php for Video Embedding for solving going to specific time in video (seek bar not work)

i'm trying to run a video through php file.
it's worked.
but the problem is that when i try to go to special time in video it replay the video from the beginning.
i have 2 php files to do this. first file named "index.php" here is it's code:

<?php
    $vids = ['asbiudsaudsausbdisa' => 'video_1'];
?>
<!DOCTYPE html>
<html>
  <body>
    <video src="videos.php?h=asbiudsaudsausbdisa" controls></video>
  </body>
</html>

second file named "videos.php" here is it's code :

<?php
$vids = ['asbiudsaudsausbdisa' => 'video.mp4'];
    
$filehash = $_GET['h'];
$filename = $vids[$filehash];
$vd = $filename; 
readfile($vd);

$file = $filename;

$filesize = filesize($file);

$offset = 0;

$length = $filesize;


if ( isset($_SERVER['HTTP_RANGE']) ) { 
// if the HTTP_RANGE header is set we're dealing with partial content
    $partialContent = true;
    // find the requested range
    // this might be too simplistic, apparently the client can request
    // multiple ranges, which can become pretty complex, so ignore it for now
    preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);

    $offset = intval($matches[1]);
    $length = intval($matches[2]) - $offset;

} else {
    $partialContent = false;
}

$file = fopen($file, 'r');

// seek to the requested offset, this is 0 if it's not a partial content request
fseek($file, $offset);

$data = fread($file, $length);

fclose($file);

if ( $partialContent ) {
    // output the right headers for partial content

    header('HTTP/1.1 206 Partial Content');

    header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
    
}
    
header('Content-Type: ' . $ctype);
header('Content-Length: ' . $filesize);
header('Content-Type: video/mp4');
header('Accept-Ranges: bytes');

?>

what i have tried and let it works but not like i want.
i run the page with this line of code first readfile($filesize); to read the file size
then i replace it by readfile($vd); then refrech the page.
it's run now and i can go to any time of the video but that's not i want .
i want to to run from the first time and works with the online videos links.

Requested Headers

GET /s/videos.php?h=asbiudsaudsausbdisa HTTP/1.1
Host: localhost
Connection: keep-alive
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
DNT: 1
Accept-Encoding: identity;q=1, *;q=0
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36
Accept: */*
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: video
Referer: http://localhost/s/
Accept-Language: en
Range: bytes=0-

Response Headers

Connection: Keep-Alive
Content-Length: 125
Content-Type: text/html; charset=UTF-8
Date: Fri, 23 Apr 2021 11:01:44 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.2
X-Powered-By: PHP/7.4.2


Solution 1:[1]

I have spended half day to solve something like that. All the answers I've found tried to manually calculate

$_SERVER['HTTP_RANGE'] 
header('HTTP/1.1 206 Partial Content');
header('Content-Range: ...); //etc ...

as in the example of this question

At least in 2022 with php 8.0 and Chrome v 101 All this is not necessary (and also does not work)

The problem:

If you put the real path/file ex: <source src="/video_folder/file_name.mp4"> everything works correctly (we are talking about the seek bar) forwards and backwards

but

If you handle the videos through a script (as in this question) <source src="/videos.php?my_variable=file_name.mp4"> the seek bar forwards and backwards never working (at least in Chrome)

Solution: videos.php script must be like this:

<?php
  $vids = ['asbiudsaudsausbdisa' => 'video.mp4'];

  $file_path = "/absolute_path/video_folder";    
  $filehash = $_GET['h'];
  $file_name = $vids[$filehash];
  $file = $file_path."/".$file_name;
  $filesize = filesize($file);

    
  header('Content-Length: ' . $filesize);
  header('Content-Type: video/mp4');
  header('Accept-Ranges: bytes');
  readfile($file);
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1