note 97367 deleted from function.readfile by danbrown
| From: | danbrown@php.net | Date: | Fri, 16 Apr 2010 12:45:11 +0000 |
| Subject: | note 97367 deleted from function.readfile by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168320@lists.php.net to get a copy of this message | ||
Note Submitter: David Friedland
----
Just a note that gaosipov's code for support for HTTP Range headers and partial downloads is
buggy. Here is a working version:
<?php
// Calculate subrange of file to send
$begin = 0;
$end = $size;
if ($_SERVER['HTTP_RANGE']) {
if (preg_match('/bytes=\h*(\d+)-(\d*)/i',
$_SERVER['HTTP_RANGE'], $matches))
{
$begin = intval($matches[1]);
if (!empty($matches[2])) {
$end = intval($matches[2]);
}
}
}
// Send HTTP response code and headers
if ($begin > 0 || $end < $size) {
header('HTTP/1.1 206 Partial Content');
} else {
header('HTTP/1.1 200 OK');
}
header('Content-Type: ' . $contentType);
header('Accept-Ranges: bytes');
header('Content-Length: ' . 1 + $end - $begin);
header("Content-Range: bytes $begin-$end/$size");
header("Content-Transfer-Encoding: binary");
// Send subrange of file
$fh = @fopen($file, 'rb');
if (!$fh) {
header('HTTP/1.0 500 Internal server error');
return;
}
fseek($fh, $begin);
$cur = $begin;
while (!feof($fh) && $cur < $end && (connection_status() == 0)) {
print fread($fh, min(1024*16, 1 + $end - $cur));
$cur += 1024 * 16;
}
?>