简体   繁体   English

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

[英]PHP - browser not caching dynamic image

I am having trouble getting my image script to cache the image in the browser (firefox). 我无法让我的图像脚本在浏览器中缓存图像(firefox)。

Here is the code : 这是代码:

$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;
    }

The image gets outputted, however after that it always goes to the else statement with the 200 response code instead of the 304. I tried to force the 304, but it looks like the image is never cached by the browser. 图像被输出,但之后它总是转到带有200响应代码而不是304的else语句。我试图强制304,但看起来图像永远不会被浏览器缓存。

Try to call it as jpg (add this in htaccess: 尝试将其称为jpg(在htaccess中添加:

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

)

don't forgot to change your_script.php and image.jpg 不要忘记更改your_script.phpimage.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM