Page not properly refreshing after image upload


I have a page which has five boxes (image spot 1, 2, 3, 4 and 5). In these boxes is where your images will be displayed if you have uploaded any.  Now, my php code and html form is on the same page.  If for example you haven't uploaded any files yet (clean profile) and you upload an image to spot 1, when the page is displayed again, you see the image in spot 1. If you upload *again* to spot 1, the upload actually worked but the old image is still displayed because it's in the browser's cache. If I press F5, then the new image is displayed.

I tried fixing it by redirecting to the same page after the upload but it didn't really do anything. Here's a snippet:

if(!(copy($_FILES['imagefile']['tmp_name'], "profiles/".substr($var__username,0,1)."/".$var__username."/" . $newfilename . "_big.jpg")))
      $ul__errorNumber = 1;
      $ul__errorMessage = "Unexpected error occured. Please try again.";
      $dst_w = 160;
      $dst_h = 120;
      $dst_img = imagecreatetruecolor($dst_w,$dst_h);
      imagejpeg($dst_img, "profiles/".substr($var__username,0,1)."/".$var__username."/" . $newfilename . ".jpg", 90);

      $dst_w = 92;
      $dst_h = 69;
      $dst_img = imagecreatetruecolor($dst_w,$dst_h);
      imagejpeg($dst_img, "profiles/".substr($var__username,0,1)."/".$var__username."/t_" . $newfilename . ".jpg", 90);

      $result = mysql_query("UPDATE tbl_personals_profile SET      profile_mainpic = 1 WHERE profile_userID = $var__userID");
      header("Location: images.php");

It's not the first time I've had problems with cache. I've done a few ASP sites and some people didn't even see the new updated content on the site.  Am I doing something wrong? Am I suppose to be using a meta tag that has the date of the page or something to check for new content?

Anyway.. I'm clueless..  hehe..  if you have any ideas - please lemme know!!!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If the page is REALLY light on images (preferrably none of more than 5k each and no more than 10 images) then I would suggest setting the file to not be cached. I don't remember the code off hand nor do I have access to it. But its supposed to be rather easy... I found this on the web though some say there is a bug in some IE versions that may make this not work... but Hopefully it will now...

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");

Good Luck and Happy coding!
Using the headers to tell browsers to reload the page doesn't seem to work all the time.

Sometimes the browser will cache it regardless of the headers you send.  One way to fix this is to force the URL to change every time by adding a random GET parameter to the URL like so:

<form action="whatever.php?random=<? echo rand(); ?>">

This way, the browser will always reload it since it will see a distinct URL (unless you get the same rand() value twice in a row).

You can rename the "random" parameter to whatever you like, and ignore it in your scripts.  It's a bit of an ugly way to do it, but it should work.
If that's the case, a good random would be md5(time()) this way the chances of getting the same number are virtually impossible. As you will never have the same time twice... though its only good on a unix system. If your not on unix you may want to look into JS function that gets the current time or soemthing like that.

Good luck with your code,
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

bloodtrainAuthor Commented:
I tried both methods mentioned and still no luck. I read a little about using the headers and apparently they work for IE only over a secure connection (https).

The trick with using a random number didn't work either.

I just noticed that if I upload a new pic, my old pic that is cached, is shown in all pages until I hit F5. It's weird - real weird.

I'm using PHP 4.3.2 on Windows XP pro as a development server and my other server is running Redhat 9. Same results on both servers.

You know, you reach a level at one point that you just wanna smash something..  ;)

The same random parameter trick probably needs to be applied to the URLs in your img src="blah" part.

It's annoying that browsers want to cache images, but it probably saves the internet a lot of load overall.

It's also a realy stupid way to fix it...

for example, you'd have this:

<img src=""></img>

instead of

<img src=""></img>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Well I hate to suggest this, but you could always try a javascript image loader.

Basically you would have files somewhere and you would put in js syntax - I don't have the script on hand sorry -

function imageLoader(){



in the body tag:
<body onload="javascript:imageLoader();">

in the document
<image name="image1" src="">
<image name="image2" src="">
<image name="image3" src="">

its a dumb way to do it and it should do the job as javascript doesn't care what is cached it just does what it is told.

Sorry I couldn't be more code specific, but its been a while since I had to actually write out a script from memory on syntax.

bloodtrainAuthor Commented:
Thanks for your help!  I looked at some sites similar to mine and I noticed that's exactly what they were doing.  :)

Now I can work on my next problem!  hehe

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.