0

I'm using a code that gets a file from the server (protected file) and download or view it on the user. I've got a problem on making Firefox and IE play HTML5 video from that URL. This code is only working of Chrome. I know its a little messy but didn't have the time to make it look better.

ob_clean();
    @ini_set('error_reporting', E_ALL & ~ E_NOTICE);
    @apache_setenv('no-gzip', 1);
    @ini_set('zlib.output_compression', 'Off');

    $file = $_GET["file"];
    $finfo = new finfo(FILEINFO_MIME);
    $mime = $finfo->file($file);
    $size = filesize($file);
    header('Content-Type: '.$mime);
    if(isset($_SERVER['HTTP_RANGE'])){
        $ranges = array_map('intval',explode('-', substr($_SERVER['HTTP_RANGE'], 6)));
        if(!$ranges[1]){
            $ranges[1] = $size - 1;
        }
        if($ranges[0]>0||$ranges[1]<$size)
          header('HTTP/1.0 206 Partial Content');
        else
          header('HTTP/1.0 200 OK');
        header('Accept-Ranges: bytes');
        header('Content-Length: ' . ($ranges[1] - $ranges[0]));
        header("Content-Disposition: inline;");
        header(sprintf('Content-Range: bytes %d-%d/%d',$ranges[0],$ranges[1],$size));
        header("Content-Transfer-Encoding: binary\n");
        header('Connection: close');
        $f = fopen($file, 'rb');
        $chunkSize = 8192;
        fseek($f, $ranges[0]);
        while(true){
            if(ftell($f) >= $ranges[1]){
                break;
            }
            echo fread($f, $chunkSize);
            @ob_flush();
            flush();
        }
    }
    else {
        header('Content-Length: ' . $size);
        @readfile($file);
        @ob_flush();
        flush();
    }

And this one doesn't work with Chrome but works with Firefox and IE.

$path = $_GET["file"];

$size=filesize($path);

$fm=@fopen($path,'rb');
if(!$fm) {
  header ("HTTP/1.0 404 Not Found");
  die();
}

$begin=0;
$end=$size;

if(isset($_SERVER['HTTP_RANGE'])) {
  if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
    $begin=intval($matches[0]);
    if(!empty($matches[1])) {
      $end=intval($matches[1]);
    }
  }
}

if($begin>0||$end<$size)
  header('HTTP/1.0 206 Partial Content');
else
  header('HTTP/1.0 200 OK');

header("Content-Type: video/mp4");
header('Accept-Ranges: bytes');
header('Content-Length:'.($end-$begin));
header("Content-Disposition: inline;");
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary\n");
header('Connection: close');

$cur=$begin;
fseek($fm,$begin,0);

while(!feof($fm)&&$cur<$end&&(connection_status()==0))
{ print fread($fm,min(1024*16,$end-$cur));
  $cur+=1024*16;
  usleep(1000);
}
die();
IkNothing
  • 1
  • 1
  • Please edit your question to provide some more context about what doesn't work and what the desired end-result should be. From what I understand of your question, you have a script for IE & Firefox, and a separate one for Chrome. So you may be looking for a way to detect which browser is requesting the video to choose the appropriate script to play the file. – Will B. Aug 15 '17 at 01:15
  • I want to create a one code that works to all browsers...I have changed headers fseek etc nothing works... – IkNothing Aug 15 '17 at 10:55

0 Answers0