note 106705 deleted from function.header by danbrown
| From: | danbrown@php.net | Date: | Wed, 30 Nov 2011 21:09:19 +0000 |
| Subject: | note 106705 deleted from function.header by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184832@lists.php.net to get a copy of this message | ||
Note Submitter: anacreo
----
Expanding on some above comments, here is a file serving mechanism with cache. I found bugs in
previous examples that every other request would not properly cache:
if (file_exists($file)) {
$mtime = filemtime($file);
$headers = getallheaders();
// if Browser sent ID, we check if they match
if (isset($headers['If-None-Match']) &&
$headers['If-None-Match'] == $mtime) {
header('HTTP/1.1 304 Not Modified');
header("ETag: {$mtime}");
header("Cache-Control: public, must-revalidate"); // HTTP/1.1
exit;
} else {
header("ETag: {$mtime}");
header("Accept-Ranges: bytes");
header("Content-Length: " . filesize($file));
header("Content-Type: " . mime_content_type($product->photo));
header("Content-Disposition: inline;
filename=\"{$product->photo}\";");
header("Cache-Control: public, must-revalidate"); // HTTP/1.1
ob_clean();
flush();
readfile($file);
exit;
}
} else {
die("Image not found" . $product->photo);
}