Link to home
Start Free TrialLog in
Avatar of bloodtrain
bloodtrainFlag for Canada

asked on

Page not properly refreshing after image upload

Hello!!!!!!!!

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.";
}
else
{
      $dst_w = 160;
      $dst_h = 120;
      $dst_img = imagecreatetruecolor($dst_w,$dst_h);
      imagecopyresized($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_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);
      imagecopyresized($dst_img,$src_img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_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!!!

Thanks!
Avatar of red010knight
red010knight
Flag of United States of America image

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!
Red010Knight
Avatar of wide_awake
wide_awake

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(); ?>">
...
</form>

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,
Red010Knight
Avatar of bloodtrain

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of wide_awake
wide_awake

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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(){

document.image1.src="imagesrc";
document.image2.src="imagesrc";
document.image3.src="imagesrc";

}

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.

Red010Knight
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