我目前正在使用下面的脚本直接从文件输出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);
}
?>
谢谢!
答案 0 :(得分:1)
我不能说为什么Safari会将您的图像视为附件/下载。我实际上在SL上的最新Safari 4中进行了测试,它运行正常。您可以尝试嗅探HTTP对话并确保没有额外的标题(例如内容处置)正在进入。此外,检查Safari配置设置可能需要付费,可能与您的其他浏览器有些不同之处?
关于表现主题:
首先,我会尝试完全消除PHP。看看您是否可以利用网络服务器的模块/配置来实现目标。
如果您不能没有PHP,那么您可以尝试让网络服务器处理实际的交付。您可以使用X-Accel-Redirect在nginx中执行此操作,使用X-Sendfile在lighttpd和apache中执行此操作。