Skip to content

Instantly share code, notes, and snippets.

@audinue
Created March 27, 2024 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audinue/9da9a36793987e0dc40318fb1ee378a7 to your computer and use it in GitHub Desktop.
Save audinue/9da9a36793987e0dc40318fb1ee378a7 to your computer and use it in GitHub Desktop.
<?php
$file = 'path-to-file';
$size = filesize($file);
$start = 0;
$end = $size - 1;
if (preg_match('/bytes=(\d+)-(\d*)/i', $_SERVER['HTTP_RANGE'] ?? '', $matches)) {
$start = $matches[1];
if (!empty($matches[2])) {
$end = $matches[2];
}
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
}
header('Accept-Ranges: bytes');
header('Content-Type: ' . mime_content_type($file));
$required = $end + 1 - $start;
$sent = 0;
$fh = fopen($file, 'r');
fseek($fh, $start);
do {
$buffer = fread($fh, 4096);
$sent += strlen($buffer);
echo $buffer;
} while ($sent < $required);
fclose($fh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment