Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Page not properly refreshing after image upload

Posted on 2003-11-02
Medium Priority
Last Modified: 2008-02-26

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

Question by:bloodtrain
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 9668792
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!

Expert Comment

ID: 9669014
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.

Expert Comment

ID: 9669134
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,
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 9672097
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..  ;)


Accepted Solution

wide_awake earned 200 total points
ID: 9674974
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="http://www.experts-exchange.com/images/woman_in_specticals.gif?blah=1200"></img>

instead of

<img src="http://www.experts-exchange.com/images/woman_in_specticals.gif"></img>

Expert Comment

ID: 9675875
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.


Author Comment

ID: 9676465
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


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question