note 74549 deleted from function.header by bjori
| From: | bjori@php.net | Date: | Tue, 17 Apr 2007 09:31:30 +0000 |
| Subject: | note 74549 deleted from function.header by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125502@lists.php.net to get a copy of this message | ||
Note Submitter: Aleksey Kuznetsov
----
Actually the code of function which uploads the file to client by "pechkin at zeos" dot
net is not completely, the HTTP server does not sends any extra-byte! It's not required to
decrease the range to 1 byte. So here is correct and fixed code:
function outload($filename, $content_type = false, $allow_cache = true, $outfilename = false) {
if (!file_exists($filename)) {
header('HTTP/1.0 404 Not Found');
exit;
}
$fh = @fopen($filename, 'rb');
if (!$fh) {
header('HTTP/1.0 403 Forbidden');
exit;
}
if (!$outfilename) $outfilename = basename($filename);
if (!$content_type) $content_type = 'application/octet-stream';
$bufsize = 20480;
$fsize = filesize($filename);
$is_partial = isset($_SERVER['HTTP_RANGE']);
if ($is_partial) // Partial download
if (preg_match("/^bytes=(\\d+)-(\\d*)$/", $_SERVER['HTTP_RANGE'],
$matches)) { // parsing Range header
$from = $matches[1];
$to = $matches[2];
if (empty($to))
$to = $fsize;
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $from-$to/$fsize");
$fsize = $to-$from;
}else {
@fclose($fh);
header("HTTP/1.1 500 Internal Server Error");
exit;
}
else
header('HTTP/1.1 200 OK');
// HEADERS
header('Accept-Ranges: bytes');
if ($content_type)
header('Content-Type: '.$content_type);
header('Content-Length: '.$fsize);
header("Content-Disposition: attachment; filename=\"$outfilename\"");
header('Content-Transfer-Encoding: binary');
if ($allow_cache)
header('Last-Modified: '.date('r', filemtime($filename)));
// CONTENT
if ($is_partial)
fseek($fh, $from);
while ($buf = fread($fh, $bufsize))
print $buf;
@fclose($fh);
exit; // stop PHP session
}