Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php serve a video (THE ONLY WORKING CODE)

$path = "./video/test.mp4";
$fp = @fopen($path, 'rb');
$size   = filesize($path); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte
header('Content-type: video/mp4');
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");
        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);
    ob_flush();
    flush();
}
fclose($fp);
exit();
Comment

PREVIOUS NEXT
Code Example
Php :: PHP sprintf — Return a formatted string 
Php :: laravel database engine innodb 
Php :: custom blade if directive 
Php :: phpmailer doesnt work on infinityfree 
Php :: select statement of table in phpmyadmin 
Php :: run new oroject laravel with idff port 
Php :: php rand between 0 and 1 
Php :: Woocommerce Adding Content to the Custom Endpoint 
Php :: php use curl 
Php :: drupal show php errors 
Php :: php mail() 
Php :: php loop through obect 
Php :: laravel sync with attributes 
Php :: magento 2 add in static block 
Php :: eloquent search ignore case 
Php :: pass the product name to form field cf7 woocommerce 
Php :: php get api 
Php :: install php56 with php73 catalina 
Php :: codeigniter 3 session not working after some time 
Php :: query log laravel 
Php :: twig render to variable 
Php :: laravel automatically encrypt model atribute 
Php :: if else in php 
Php :: how to get full path of uploaded file in php 
Php :: Detect Mobile Platform On Php 
Php :: XAMPP PHPMyAdmin Access 
Php :: creating jobs laravel 
Php :: model not found laravel 
Php :: php email sender 
Php :: cakephp 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =