note 97367 added to function.readfile
| From: | DavidFriedland at osu1 dot php dot net | Date: | Fri, 16 Apr 2010 00:12:49 +0000 |
| Subject: | note 97367 added to function.readfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168307@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 208.69.120.55
Probable Submitter: 66.104.252.161
----
Manual Page -- http://www.php.net/manual/en/function.readfile.php
Edit -- https://master.php.net/note/edit/97367
Del: integrated -- https://master.php.net/note/delete/97367/integrated
Del: useless -- https://master.php.net/note/delete/97367/useless
Del: bad code -- https://master.php.net/note/delete/97367/bad+code
Del: spam -- https://master.php.net/note/delete/97367/spam
Del: non-english -- https://master.php.net/note/delete/97367/non-english
Del: in docs -- https://master.php.net/note/delete/97367/in+docs
Del: other reasons-- https://master.php.net/note/delete/97367
Reject -- https://master.php.net/note/reject/97367
Search -- https://master.php.net/manage/user-notes.php