Disable cache for some images

前端 未结 13 1149

I generate some images using a PHP lib.

Sometimes the browser does not load the new generated file.

How can I disable cache just for images created dynamica

13条回答
  •  旧时难觅i
    2020-11-22 08:17

    I checked all the answers and the best one seemed to be (which isn't):

    
    

    at first.

    However, if you add cache=none parameter (which is static "none" word), it doesn't effect anything, browser still loads from cache.

    Solution to this problem was:

    
    

    where you basically add unix timestamp to make the parameter dynamic and no cache, it worked.

    However, my problem was a little different: I was loading on the fly generated php chart image, and controlling the page with $_GET parameters. I wanted the image to be read from cache when the URL GET parameter stays the same, and do not cache when the GET parameters change.

    To solve this problem, I needed to hash $_GET but since it is array here is the solution:

    $chart_hash = md5(implode('-', $_GET));
    echo "";
    

    Edit:

    Although the above solution works just fine, sometimes you want to serve the cached version UNTIL the file is changed. (with the above solution, it disables the cache for that image completely) So, to serve cached image from browser UNTIL there is a change in the image file use:

    echo "";
    

    filemtime() gets file modification time.

提交回复
热议问题