Cannot play video on IE and Firefox with PHP header and readfile/fopen -
i'm using code gets file server (protected file) , download or view on user. i've got problem on making firefox , ie play html5 video url. code working of chrome. know little messy didn't have time make 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 1 doesn't work chrome works firefox , 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();
Comments
Post a Comment