Skip to content

Instantly share code, notes, and snippets.

@codler
Created October 17, 2012 17:18
Show Gist options
  • Save codler/3906826 to your computer and use it in GitHub Desktop.
Save codler/3906826 to your computer and use it in GitHub Desktop.

Revisions

  1. codler renamed this gist Oct 17, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. codler created this gist Oct 17, 2012.
    66 changes: 66 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    <?php
    # Nginx don't have PATH_INFO
    if (!isset($_SERVER['PATH_INFO'])) {
    $_SERVER['PATH_INFO'] = substr($_SERVER["ORIG_SCRIPT_FILENAME"], strlen($_SERVER["SCRIPT_FILENAME"]));
    }

    $request = substr($_SERVER['PATH_INFO'], 1);
    $file = $request;
    $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/mp4');
    header("Accept-Ranges: 0-$length");
    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");
    exit;
    }
    if ($range == '-') {
    $c_start = $size - substr($range, 1);
    }else{
    $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");
    exit;
    }
    $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;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
    }

    fclose($fp);
    exit();
    ?>