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
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.

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

Guest prozente

kid_drew, you would output the content-type for the image then readfile would do the trick

 

 

header("Content-Type: image/gif");

readfile('myimage.gif');

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.