Created
March 27, 2024 12:18
-
-
Save audinue/9da9a36793987e0dc40318fb1ee378a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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