note 90459 added to function.readfile
| From: | sakaiatd4kdotnet at osu1 dot php dot net | Date: | Thu, 23 Apr 2009 12:25:50 +0000 |
| Subject: | note 90459 added to function.readfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153611@lists.php.net to get a copy of this message | ||
To reduce the burden on the server, you might want to output "Etag" and/or
"Last-Modified" on http response header. But there are some headers, which PHP itself
outputs automatically, disturbing this. So I wrote this function with erasing these.
If you guys know how to judge the return values of function "stat", in order to avoid
using "is_file" or "is_readable" (or "is_dir"), please let me know or
just write it here.
If you don't have to do anything special on 404, "header('HTTP/1.x xxx
xxxxx');" can be inside of the function.
<?php
$filename = '/foo/bar/myfeed.rss';
$http_stat_code = readfile_if_modified($filename, array('Content-Type: text/xml'));
switch($http_stat_code) {
case 404:
header('HTTP/1.0 404 Not Found');
echo '<html><head></head><body><a href="http://example.com/">http://example.com/<a></body></html>';
exit;
default:
header('X-System-Url: http://example.com/',
true, $http_stat_code);
}
function readfile_if_modified($filename, $http_header_additionals = array()) {
if(!is_file($filename)) {
// header('HTTP/1.0 404 Not Found');
return 404;
}
if(!is_readable($filename)) {
// header('HTTP/1.0 403 Forbidden');
return 403;
}
$stat = @stat($filename);
$etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'],
$stat['mtime'] * 1000000);
header('Expires: ');
header('Cache-Control: ');
header('Pragma: ');
if(isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
$_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('Etag: "' . $etag . '"');
// header('HTTP/1.0 304 Not Modified');
return 304;
} elseif(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']) {
header('Last-Modified: ' . date('r', $stat['mtime']));
// header('HTTP/1.0 304 Not Modified');
return 304;
}
header('Last-Modified: ' . date('r', $stat['mtime']));
header('Etag: "' . $etag . '"');
header('Accept-Ranges: bytes');
header('Content-Length:' . $stat['size']);
foreach($http_header_additionals as $header) {
header($header);
}
if(@readfile($filename) === false) {
// header('HTTP/1.0 500 Internal Server Error');
return 500;
} else {
// header('HTTP/1.0 200 OK');
return 200;
}
}
?>
----
Server IP: 61.195.146.164
Probable Submitter: 219.118.191.50
----
Manual Page -- http://www.php.net/manual/en/function.readfile.php
Edit -- https://master.php.net/note/edit/90459
Del: integrated -- https://master.php.net/note/delete/90459/integrated
Del: useless -- https://master.php.net/note/delete/90459/useless
Del: bad code -- https://master.php.net/note/delete/90459/bad+code
Del: spam -- https://master.php.net/note/delete/90459/spam
Del: non-english -- https://master.php.net/note/delete/90459/non-english
Del: in docs -- https://master.php.net/note/delete/90459/in+docs
Del: other reasons-- https://master.php.net/note/delete/90459
Reject -- https://master.php.net/note/reject/90459
Search -- https://master.php.net/manage/user-notes.php