Jump to content

Force image reload in php


kid_drew

Recommended Posts

So for the project I'm working on, I have an image that gets converted from an eps file fairly regularly.  When you go to the page that displays that image (the gif, not the eps) it isn't aware that the image file has been updated in the background and it always loads the cached file unless I push refresh/reload.  Is there a way in PHP/Apache to get that file to always load from the server rather than from cache?

 

Or maybe in Javascript?

Link to comment
https://forums.phpfreaks.com/topic/46909-force-image-reload-in-php/
Share on other sites

Guest prozente

Take a look at the header page in the php manual, the below example is there

 

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

 

You would output these headers and have the php script then output the image and you would instead reference the script instead of the actual image.

if you put an ajax script, which loads the pic, that'd kill the cache, or... if you just tell the header() to not use cache... both work :-)

 

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Cache-Control: post-check=0, pre-check=0", false);

header("Pragma: no-cache");

 

there dies any/all forms of cache out there :-)

Headers don't seem to be doing the trick.  I read somewhere that the header cache calls are simply "suggestions" and don't actually force anything.  How ridiculous is it that this is so diffucult?

 

I've even tried the trick of making the image call unique like this:

 

<img src="foo.gif?<?php echo rand(1,3000)?>">

 

And that doesn't even work.

 

How do I make php display the image?

 

if you put an ajax script, which loads the pic, that'd kill the cache, or... if you just tell the header() to not use cache... both work :-)

 

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Cache-Control: post-check=0, pre-check=0", false);

header("Pragma: no-cache");

 

there dies any/all forms of cache out there :-)

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

echo '<img src="img.jpg">';

that should disable any cache on the page, telling the browser to reload the img each opening...

That's using html to display the image, and that isn't working.

 

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

echo '<img src="img.jpg">';

that should disable any cache on the page, telling the browser to reload the img each opening...

Give me an example of an ajax script that would do this.  Note that this image is not the only thing I'm displaying on the page and it will have to be embedded in html.  Is that possible?

 

 

 

if you put an ajax script, which loads the pic, that'd kill the cache, or... if you just tell the header() to not use cache... both work :-)

 

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Cache-Control: post-check=0, pre-check=0", false);

header("Pragma: no-cache");

 

there dies any/all forms of cache out there :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.