bloodtrain
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['imagefi le']['tmp_ name'], "profiles/".substr($var__u sername,0, 1)."/".$va r__usernam e."/" . $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,$ds t_w,$dst_h ,$src_w,$s rc_h);
imagejpeg($dst_img, "profiles/".substr($var__u sername,0, 1)."/".$va r__usernam e."/" . $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,$ds t_w,$dst_h ,$src_w,$s rc_h);
imagejpeg($dst_img, "profiles/".substr($var__u sername,0, 1)."/".$va r__usernam e."/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!
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['imagefi
{
$ul__errorNumber = 1;
$ul__errorMessage = "Unexpected error occured. Please try again.";
}
else
{
$dst_w = 160;
$dst_h = 120;
$dst_img = imagecreatetruecolor($dst_
imagecopyresized($dst_img,
imagejpeg($dst_img, "profiles/".substr($var__u
$dst_w = 92;
$dst_h = 69;
$dst_img = imagecreatetruecolor($dst_
imagecopyresized($dst_img,
imagejpeg($dst_img, "profiles/".substr($var__u
$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!
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?rando m=<? 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.
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?rando
...
</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
Good luck with your code,
Red010Knight
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.. ;)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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="image src";
document.image2.src="image src";
document.image3.src="image src";
}
in the body tag:
<body onload="javascript:imageLo ader();">
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
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="image
document.image2.src="image
document.image3.src="image
}
in the body tag:
<body onload="javascript:imageLo
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
ASKER
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
Now I can work on my next problem! hehe
// 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