0

What I'm trying to do?

I have an admin page where I can change images - they all use fixed names, and are uploaded directly to the file system, but sometimes when I upload it just displays an old image.

At the same time I don't want to download the pages over and over again, as it will be just slow and ugly.

So what I want is to make no cache for the actual output text in my php, but for the images to make it revalidate the creation date, and if it's more recent than stored in cache to download a new one, otherwise to load it from cache.

Please advice what headers I would best use for that purpose, or if it's impossible to make it that way..

Thanks :)

Anonymous
  • 4,692
  • 8
  • 61
  • 91
  • _"What I'm trying to do?"_ It's spelled "What have you tried?". Take a look [here](http://stackoverflow.com/a/2417758/266143), for example. – CodeCaster Jun 07 '12 at 14:50
  • Why downvote? Obviously I tried some things. header("Cache-Control: must-revalidate"); for example, but its just doesn't yield any outcome, so I think I'm getting it wrong somehow – Anonymous Jun 07 '12 at 14:51
  • _"Obviously I tried some things"_ which we cannot see. So instead of asking us to show a million possible solutions, show what you have found and what isn't working there. – CodeCaster Jun 07 '12 at 14:52
  • as I said I tried to play around with header option. which I believe is reasonable to assume to be used in my case. so please remove the downvote – Anonymous Jun 07 '12 at 14:54
  • 1
    There's about ten HTTP headers you can use for caching and invalidation of that, of which a few are used in the [example](http://stackoverflow.com/a/2417758/266143) I linked. We cannot guess what to fix in your code order to make it work, you need to show code and [some research effort](http://stackoverflow.com/search?q=php+cache+image). – CodeCaster Jun 07 '12 at 14:56
  • THanks, I'll review into that – Anonymous Jun 07 '12 at 14:59
  • Edit: these examples are completely different and don't provide any solution. Ad I've said the images are static, not dynamc – Anonymous Jun 07 '12 at 15:08
  • You never said they were static; if that is the case, your web server should handle the cache invalidation when you overwrite the image files. – CodeCaster Jun 07 '12 at 15:11
  • Well I'm using Apache. It does handle if I re-create the file. ie if it has a new creation date. But if the image is simply modified and retains the same creation date - it does not necessary invalidates the cache. – Anonymous Jun 07 '12 at 15:17
  • clear your browser cache, request an image, and post to use the `full response headers`. – goat Jun 07 '12 at 16:23

1 Answers1

3

An easy way to do this is to append a query string to the end of the image file name and changing it each time the image changes. The query string is ignored by the image file so it won't affect it at all but to the browser it is part of the image name so when it changes it forces the browser to get the new image.

For example, you can call an image someimage.png?1. When the image is updated you then would call it someimage.png?2.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Yes, this shall defenitely work, I've used this method before in another page. But it sould be difficult to implement though. as we would have to store these counters somewhere... – Anonymous Jun 07 '12 at 14:59
  • I have sometimes embedded the index number into the file name itself, then moved it into the query string using an Apache rewrite. – Magmatic Jun 24 '13 at 19:09
  • I am using this method too, but IE (actually any version) seems to ignore it sometimes, it work to 80% but every few image refreshes, id simply does not work.. – Asped Oct 13 '13 at 20:47