How to clear browser cache when re-uploading image with same filename, in php?

后端 未结 8 1500
灰色年华
灰色年华 2021-02-18 19:05

I have a listing of items, that also contains an image thumbnail. Sometimes I need to change the thumbnail image, but keep the filename the same. Is there a way to force the bro

8条回答
  •  情书的邮戳
    2021-02-18 19:31

    When sending out the image, make sure not to send any Expires header. Also consider sending this cache header:

    Cache-Control: must-revalidate
    

    This will make the browser ask your server for the image every time. Watch for an If-Modified-Since header in the request; if the image is not modified, answer with an 304 HTTP code.

    This whole procedure (must-revalidate, If-Modified-Since, 304 answer) will make it possible for the browser to cache the image content but at the same time ask your server if the file has changed.


    Another, maybe simpler, solution is to only set an Expires header a short time in the future, for example ten minutes, and inform the user of the ten minute delay. This will speed up most page loads since there's no need to do any HTTP request for the image at all.

提交回复
热议问题