PHP - 浏览器不缓存动态图像

3
我遇到了一个问题,无法在浏览器(Firefox)中缓存我的图片脚本。

以下是代码:

$type = 'image/jpeg';
$image = '../../files/image.jpeg';

    header("Content-type: $type"); 
    header("Cache-Control: private, max-age=10800, pre-check=10800");
    header("Pragma: private");
    header("Expires: " . date(DATE_RFC822,strtotime("1 week")));

    if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
      header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
      exit;
    } else {
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($image)).' GMT', true, 200);
    echo @readfile($image);
    exit;
    }

图像被输出,但之后它总是进入else语句,并返回200响应代码,而不是304。我试图强制使用304,但似乎浏览器从未缓存该图像。

尝试这个答案:https://dev59.com/u3A85IYBdhLWcg3wCe9Z#3001556 - OZ_
1
那意味着服务器没有设置HTTP_IF_MODIFIED_SINCE - Jason McCreary
1个回答

0

尝试将其命名为jpg(在htaccess中添加此内容:

RewriteEngine on
RewriteRule image.jpg your_script.php [L]

)

别忘了更改 your_script.phpimage.jpg


2
不应该有影响,因为他发送了Content-type。 - Andre Backlund

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接