freelance84 Posted January 18, 2012 Share Posted January 18, 2012 Hi, it's been a while since i was on here... luckily i've been very busy .. I have a slight issue to do with the caching of images in a CMS service site I built: A client to the CMS site can upload images The client has the option to upload new and overwrite their existing images By the architecture of the system, if an image is uploaded to replace an existing one its name is kept the same, eg image0023.jpg This posed a problem with the browsers caching, it meant that although the image had changed on the server, the browser was not downloading the new as it thought it already had it in the cache... As a work around to just using a blanket no-cache meta I used the method which places a random string after the image source: <img src="image0023.jpg?1234567"/> When the page is reloaded the random string is no longer there on the image. THE RESULT: 75% of the time it all works correctly and the correct images are displayed... however sometimes when the ?123456 is removed after a page reload the browser goes ahead and displays the old image that was cached... MY POSSIBLE SOLUTION (for the 25% fail) Accompany all images to be displayed on screen with their own unique ?randomString and store the said strings in a database along with the clients id and image id. The said strings to be a time stamp, or something similar, and renewed each time the image is replaced or edited. Hopefully this will mean the browser will cache more than just the striaght forward file name thus never display an out of date image and as there is no meta no-cache my bandwidth may be saved a little... I'm very keen to here what other peoples opinions on this might be, or if there is another way or better way or issues i may hit trying the above solution... Thanks a lot, John Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/ Share on other sites More sharing options...
scootstah Posted January 18, 2012 Share Posted January 18, 2012 You could send headers to expire the browser cache, which means it should download the new images. Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/#findComment-1308763 Share on other sites More sharing options...
freelance84 Posted January 18, 2012 Author Share Posted January 18, 2012 But if I did that then the clients browser would be forced to download the entire site again from scratch would it not, ie all js, css and all images... I've been hunting for a while now but i cannot find a way to force browsers to clear a specific caches items and the above is the only solution i can think of Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/#findComment-1308837 Share on other sites More sharing options...
scootstah Posted January 18, 2012 Share Posted January 18, 2012 Sure, but it's not that big of a deal. They probably won't even notice. Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/#findComment-1308925 Share on other sites More sharing options...
freelance84 Posted January 18, 2012 Author Share Posted January 18, 2012 True, however its a new project on a cloud where we pay per downloaded gig so it is not the clients i'm worried about... it my server and my wallet Cheers for the tip though Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/#findComment-1308933 Share on other sites More sharing options...
scootstah Posted January 18, 2012 Share Posted January 18, 2012 The only other options is to either change the image name or do what you are doing with the query string. But instead of a random string, use filemtime(). Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/#findComment-1308962 Share on other sites More sharing options...
freelance84 Posted January 18, 2012 Author Share Posted January 18, 2012 Ah brilliant!! That solves that... Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/255258-cms-image-cache-workaround/#findComment-1309066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.