note 74549 added to function.header
| From: | AlekseyKuznetsov at osu1 dot php dot net | Date: | Tue, 17 Apr 2007 02:27:18 +0000 |
| Subject: | note 74549 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125487@lists.php.net to get a copy of this message | ||
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
}
----
Server IP: 195.248.191.91
Probable Submitter: 194.143.145.214
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- https://master.php.net/note/edit/74549
Del: integrated -- https://master.php.net/note/delete/74549/integrated
Del: useless -- https://master.php.net/note/delete/74549/useless
Del: bad code -- https://master.php.net/note/delete/74549/bad+code
Del: spam -- https://master.php.net/note/delete/74549/spam
Del: non-english -- https://master.php.net/note/delete/74549/non-english
Del: in docs -- https://master.php.net/note/delete/74549/in+docs
Del: other reasons-- https://master.php.net/note/delete/74549
Reject -- https://master.php.net/note/reject/74549
Search -- https://master.php.net/manage/user-notes.php