<?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(); | |
?> |
This comment has been minimized.
This comment has been minimized.
Thanks for the code is very useful. |
This comment has been minimized.
This comment has been minimized.
Change |
This comment has been minimized.
This comment has been minimized.
Thanks, I don't know if it was that or video problem. I think the large file was corrupted. |
This comment has been minimized.
This comment has been minimized.
Hello, |
This comment has been minimized.
This comment has been minimized.
You saved my day! Thanks! |
This comment has been minimized.
This comment has been minimized.
Just want to note that there seems to be a problem when using webm video files instead of mp4. Many network requests appear but the video does not continue to play. Unfortunately I could not find a solution for this issue. |
This comment has been minimized.
This comment has been minimized.
@q2apro see https://github.com/diversen/http-send-file I use this to send video(mp4/webm/ogv) |
This comment has been minimized.
This comment has been minimized.
thank you very much! this the only code I found that works! |
This comment has been minimized.
This comment has been minimized.
absolutely awesome! thanks for that! |
This comment has been minimized.
This comment has been minimized.
Python code, to download based on range header.url = "http://changeyour-range-based-URL.snippetbucket.com/5MB.zip" |
This comment has been minimized.
This comment has been minimized.
That is really good script. What if we've hosted the video on s3/oss(amazon/alicloud) bucket instead of our server. For that case this script will work? |
This comment has been minimized.
This comment has been minimized.
Works well in Chrome and firefox, but not working in Safari! |
This comment has been minimized.
This comment has been minimized.
Hi, i'm having a problem with this script (And many others i found) it works excellent for most of my videos, but i found a problem with large files.. For example if the file is about 300mb everything is ok.. but i tried a HD mp4 h264 video that is > 1h and 2.5 gigs and there's no way to make it play.. Is not a coding problem .. First thing this code fix the filesize 32bit limitation for larger files:
But then don't know if it's a fopen and must use CURL, not sure if is the fseek handling large files.. someone with more experience than me can check and test this code for larger files? would be great! Happy Coding! |
This comment has been minimized.
This comment has been minimized.
Some many years later...thank you so much, this is awesome! |
This comment has been minimized.
This comment has been minimized.
Everything works fine but for some reason I don't know, when I start playing a video and then click on a link on my page or use the browser's back button, the browser freezes and waits a while ( variable ) before following the link. The profiler indicates that the requested page change is in pending status. And finally if I reload that page, the issue doesn't appears anymore. Seems that chrome have cached the video. Any idea ? EDIT Any idea ? EDIT2 |
This comment has been minimized.
This comment has been minimized.
Thanks @Rob--W, this saved me a bunch of time |
This comment has been minimized.
This comment has been minimized.
Great work, thank you very much |
This comment has been minimized.
This comment has been minimized.
@fadiabualnaser
|
This comment has been minimized.
This comment has been minimized.
Great! You saved my day too! |
This comment has been minimized.
This comment has been minimized.
Thank you so much! Works! |
This comment has been minimized.
This comment has been minimized.
Han Lin Yap great work. Thank you for sharing. |
This comment has been minimized.
Epic, this is the only thing I have found that works!
Props to you!