PHP JPEG头阻止图像下载

PHP JPEG头阻止图像下载,php,apache,http,photo,Php,Apache,Http,Photo,我目前正在使用下面的脚本直接从文件中输出JPEG的内容。我这样做的原因是因为我使用mod_rewrite/php来屏蔽原始文件的名称。除了Safari坚持下载照片,而不是只允许我在浏览器中查看照片之外,这部分工作与预期相符。我丢了头球了吗?另外,我可以做哪些修改来提高性能? error_reporting(0); // Find which image we're trying to get $_GET['id']; // In the real script

我目前正在使用下面的脚本直接从文件中输出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);

    }

?>

谢谢

我说不出为什么Safari会将您的图像视为附件/下载。实际上,我在最新的Safari 4 SL上对它进行了测试,效果良好。您可以尝试嗅探HTTP对话,并确保没有额外的头(例如内容处置)悄悄进入。此外,检查Safari配置设置也可能值得,也许有些微妙之处与您的其他浏览器有所不同

关于绩效主题:

首先,我将尝试完全消除PHP。看看是否可以利用Web服务器的模块/配置来实现您的目标


如果没有PHP不行,那么可以尝试让Web服务器处理实际交付。您可以在使用X-Accel-Redirect的nginx中以及使用X-Sendfile的lighttpd和apache中执行此操作。

+1我同意,在请求普通映像时以及通过php请求时,尝试嗅探标头,并检查是否存在差异