我目前正在使用下面的脚本直接从文件中输出 JPEG 的内容。我这样做的原因是因为我使用 mod_rewrite/php 来掩盖原始文件的名称。这部分按预期工作,除了 Safari 坚持下载照片而不是只允许我在浏览器中看到它。我错过了标题吗?另外,我可以进行哪些修改来提高性能?
error_reporting(0);
// Find which image we're trying to get
$_GET['id'];
// In the real script, I do some proccessing here
$filename = $_GET['id'] . '.jpg';
// Getting headers sent by the client.
$headers = apache_request_headers();
// Checking if the client is validating his cache and if it is current.
if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == filemtime($filename))) {
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($filename)).' GMT', true, 304);
} else {
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($filename)).' GMT', true, 200);
header('Content-Length: '.filesize($filename));
header('Content-Type: image/jpeg');
print file_get_contents($filename);
}
?>
谢谢!