Page 1 of 1
PHP: Prevent image caching, new/uploaded avatars
Posted: Mon Aug 25, 2008 11:53 am
by JAB Creations
How can I prevent images from being cached in the browser using PHP?
The scenario is simple: I have created the ability to upload an avatar image. However when I upload different images I still see the previous image. I am using the same file name
however it's predicable and creates less server load. So I'm only interested in preventing the image from being loaded in the cache only on the following condition:
$_FILES['image_avatar']['name'] != "". I would not mind in example trying to use a random generated path that the server would still point to the same image though since the browser would see a different path it would then not bother with the cached version.
I did try this though without any luck...
Code: Select all
header('Expires: Tue, 22 May 1988 03:20:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, n0-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
Suggestions please?
Re: PHP: Prevent image caching, new/uploaded avatars
Posted: Tue Aug 26, 2008 2:36 am
by panic!
a quick solution would be:
the_real_file.jpg?id=39390432
39390432 being a random number or..
/image/39390432/the_real_file.jpg
and use a rewrite rule in an .htaccess file:
Code: Select all
RewriteEngine On
RewriteRule image/(.*)/(.*) /images/$2.jpg
obviously I don't know your directory structure so you'd have to amend this to reflect your directory structure.
Re: PHP: Prevent image caching, new/uploaded avatars
Posted: Tue Aug 26, 2008 12:02 pm
by JAB Creations
I'm not seeing how the server will know which file is the image requested. What does the Apache code do in plain English?
Re: PHP: Prevent image caching, new/uploaded avatars
Posted: Tue Aug 26, 2008 3:49 pm
by panic!
A please or thank you would be nice..Manners cost nothing...
Re: PHP: Prevent image caching, new/uploaded avatars
Posted: Tue Aug 26, 2008 4:30 pm
by JAB Creations
I consider myself a bit more keen to ensuring people who answer my questions are thanked. Sure now looking back I should have added "Could you please clarify..." to the post. I do thank you for taking the time to answer it however if I don't comprehend the answer I'll try to communicate for a clarification first. It was not my intent to be rude in any sense. I usually follow up most final posts with examples or at least a connect the dots sort of reply so others viewing the archive threads I have started or participated in can comprehend what I did without having to second guess...I reserve the thanks once at least some two-way communication has been successful.
So could you 011100000110110001100101011000010111001101100101 clarify what Apache is doing as it's syntax is too minimalistic for me to comprehend thus far at this moment in time?
Re: PHP: Prevent image caching, new/uploaded avatars
Posted: Tue Aug 26, 2008 7:39 pm
by Zoxive
JAB Creations wrote:
So could you 011100000110110001100101011000010111001101100101 clarify what Apache is doing as it's syntax is too minimalistic for me to comprehend thus far at this moment in time?
Code: Select all
RewriteEngine On
RewriteRule image/(.*)/(.*) /images/$2.jpg
All that is doing is routing to the image file, but it has a random generated number in between to trick the browser into thinking its a different file.
The only thing is the regex for that is incorrect. Unless your file names are myname.jpg.jpg
Code: Select all
RewriteEngine On
RewriteRule image/(.*)/(.*)\.jpg /images/$2.jpg
## we only want .jpg once
But i would just recommend the other way. Less work to do.
my/folders/to/image.jpg?v=30294
I use this solution for most of my images. Because i tell the browser to cache them for a month, so the "Version" (?v=(num)) number doesn't change, I change them when i update the images.
Re: PHP: Prevent image caching, new/uploaded avatars
Posted: Wed Aug 27, 2008 9:42 am
by panic!
oh yeah, two file extensions: my bad! Thanks for pointing that out Zoxive.
'JAB Creations' definitely deserves his most peculiar award.