Skip to content

Instantly share code, notes, and snippets.

@carloscarucce
Created October 13, 2025 12:15
Show Gist options
  • Select an option

  • Save carloscarucce/8b8876a02d49170580f097d284454c49 to your computer and use it in GitHub Desktop.

Select an option

Save carloscarucce/8b8876a02d49170580f097d284454c49 to your computer and use it in GitHub Desktop.
Video stream script
<?php
// Path to your protected video file (outside web root recommended)
$videoFile = '/path/to/your/private/video.mp4';
// Check if the file exists
if (!file_exists($videoFile)) {
header("HTTP/1.1 404 Not Found");
exit;
}
// Get file size and mime type
$size = filesize($videoFile);
$mime = "video/mp4"; // Change if needed (e.g., video/webm)
// Headers to prevent download and force inline streaming
header("Content-Type: $mime");
header("Content-Length: $size");
header("Content-Disposition: inline; filename=\"video.mp4\"");
header("Accept-Ranges: bytes");
// Handle partial content (for seeking)
if (isset($_SERVER['HTTP_RANGE'])) {
$range = $_SERVER['HTTP_RANGE'];
$range = str_replace("bytes=", "", $range);
$range = explode("-", $range);
$start = intval($range[0]);
$end = $range[1] ? intval($range[1]) : $size - 1;
$length = $end - $start + 1;
header("HTTP/1.1 206 Partial Content");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
$fp = fopen($videoFile, "rb");
fseek($fp, $start);
$buffer = 1024 * 8;
while (!feof($fp) && ($pos = ftell($fp)) <= $end) {
if ($pos + $buffer > $end) {
$buffer = $end - $pos + 1;
}
echo fread($fp, $buffer);
flush();
}
fclose($fp);
} else {
readfile($videoFile);
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment