Loading HTML5 video with PHP Chunks or Not

Lately the google stopped working for me.

Anyways… I have a simple question, should I serve a video file with PHP Chunks (through PHP file), or NOT (using original link to full mp4/webm video file).

Is one solution better than the other one, how is the file loaded?
Isn’t the user experience the same and one is not faster than the other?
Are they loading files differently in perspective of performance and bandwidth, also the servers CPU and memory?

If anyone knows one of the answers, then please share it with me.

Thank you!

I have learned that serving video files with PHP is good for:


  1. Blocking unauthorized requests (login systems).
  2. Blocking abusive requests that consume too many bandwidth or repeatedly try to download one “large” video file (keep track of that with PHP).
  3. Make original video file download harder. (that is not important, because there never ever will be a complete solution in this WWW)

Serving a “MP4” video file with PHP takes a lot more resources, but “the control you get is more important”???


However, let see how we can solve these points by not using PHP and free up a lot of server resources.

  1. You can block direct file requests with .htaccess Apache directives, once you block the user from your main site, it can not access the video file.
  2. You actually do not need file level blocking for unauthorized requests, because direct file requests are blocked with .htaccess and private files get new filename anyways (regular login system on your site works fine).

But still there is one more things to consider using Apache directives:
HTTP_REFERER can be faked and by sending with a some sort of software browser the accepted referrer to a direct file request, the bandwidth attacks can be still done. You made it a little bit harder.


So finally the only real reason to use PHP to serve a video file like MP4 is to block attacks?

Agree or refute?

Thank you!

I hope everyone still healthy…

So finally I got the coding done.

So I use both Apache (for sites who like to display my video will get blocked with wrong header others are all allowed) and am using PHP token and expiration time, so serve the video with PHP, chunk by chunk.

Found some codes on the web to serve mp4 with PHP, these codes were almost perfect, but found some bugs or something in there I didn’t like. I added a little bit more breath to it like this (I added the S count system in While, I hope this does not cause any troubles in the long run):

///working part of the code
if (file_exists($file)) {
    $fp = @fopen($file, 'rb');
    $size   = filesize($file); // File size
    $length = $size;           // Content length
    $start  = 0;               // Start byte
    $end    = $size - 1;       // End byte
    header('Content-type: video/'.$filetype.'');
    //header("Accept-Ranges: 0-$length");
    header("Accept-Ranges: bytes");
    if (isset($_SERVER['HTTP_RANGE'])) {
        $c_start = $start;
        $c_end   = $end;
        list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
        if (strpos($range, ',') !== false) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
        if ($range == '-') {
            $c_start = $size - substr($range, 1);
            $range  = explode('-', $range);
            $c_start = $range[0];
            $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
        $c_end = ($c_end > $end) ? $end : $c_end;
        if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
        $start  = $c_start;
        $end    = $c_end;
        $length = $end - $start + 1;
        fseek($fp, $start);
        header('HTTP/1.1 206 Partial Content');
    header("Content-Range: bytes $start-$end/$size");
    header("Content-Length: ".$length);
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
 if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
 //take a break start/my modification
 echo fread($fp, $buffer);
if ($s >= 500) {
 //take a break
} else {
//file does not exists:
 } else  {
		  header("HTTP/1.1 404 Not Found");

or else the PHP will stay mostly alive(stuck in use) until file is completely loaded or almost loaded, this way it loads by small chunks and does free the PHP process immediately, also if you want to download the file you need to download all 50 pieces or something like that. (one PHP process per user).

I use session system for security and performance (one session one file principle). So if you do not have cookies you can not watch videos. You can watch one video at a time etc

Performance is everything, I like fast servers who doesn’t, right?

Anyways I tested this code on iPhone safari, Firefox, chrome. Seems to be working fine.

If anyone can give any suggestions how to improve/fix this any further would be great!

Wish you all well

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.