contra10 Posted April 1, 2009 Share Posted April 1, 2009 I'm saving pics in my db and I have an upload section, and beside the upload form it shows the current picture. When I upload the pic the pic gets entered into the db but the pic is not uploaded at the side. When i go to the profile page where the pic is being displayed the old one is showed. Only after I refresh the browser does the new pic become visible. Any ideas as to why this is like that? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted April 1, 2009 Share Posted April 1, 2009 If I am guessing it right, it's because of cache. Whenever a page is loaded, it may create a cache to it so that the next you visit their, it won't get all those other things repeatedly. Quote Link to comment Share on other sites More sharing options...
Q695 Posted April 1, 2009 Share Posted April 1, 2009 Are you running the update before it grabs the image? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 1, 2009 Share Posted April 1, 2009 You can trick browsers into thinking you are displaying new content and forcing them to load the image again with a simple trick - add something random to the url of the image! Let's take normal use of IMG. <img src="gfx/pic.jpg" /> pic.jpg would be cached and if you change it chances are the old one could appear for a while still so we need to change the name of the file. This is really very easy to do by adding a random number on the end just like we're passing the picture a number. <img src="gfx/pic.jpg?r=<?php echo mt_rand(1,5000); ?>" /> Now we're adding a random number onto the URL we've got a 1 in 5000 chance of getting the same number so this effectively makes the filename look similar to this: gfx/pic.jpg?r=451 gfx/pic.jpg?r=3264 gfx/pic.jpg?r=3286 gfx/pic.jpg?r=162 Quote Link to comment Share on other sites More sharing options...
contra10 Posted April 1, 2009 Author Share Posted April 1, 2009 i tried the mt_rand but it doesn't work. The image is shown before the upload as well as after the upload. my code is like this <div class="left"> <?php include('settings.php'); // Make sure the user actually // selected and uploaded a file if(isset($_COOKIE['ID_my_site'])) { $usernamecookie = $_COOKIE['ID_my_site']; } if(is_numeric($_GET['user'])){ $id = $_GET['user']; } $name = mysql_real_escape_string($_POST['usernamecookie']); $idpic = mysql_real_escape_string($_POST['iduser']); if (isset($_POST['submit']) && ($_FILES['image']) && $_FILES['image']['size'] > 0) { // Temporary file name stored on the server $tmpName = $_FILES['image']['tmp_name']; $fileType = $_FILES['image']['type']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); // Create the query and insert // into our database. $query = "UPDATE `tbl_images` SET `image` = '" . $data . "', `filetype` = '" . $fileType . "' WHERE `userid` = '" . $idpic . "'"; $results = mysql_query($query) or die(mysql_error()); // Print results print "Thank you, your file has been uploaded."; // Create the URL string $url = "http://www.url.com/image/updatemain.php?user=$idpic"; // Finall Echo the meta tag echo('<meta HTTP-EQUIV="REFRESH" content="0; url='.$url.'">'); }else{ echo "Please do not refresh page or double click on upload. Allow time for download."; } // Close our MySQL Link mysql_close($link); ?> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="512000000000" type="hidden"> <input name="image" accept="image/jpeg" type="file"> <tr><td><?php echo "<input type='hidden' name='usernamecookie' value='$usernamecookie'>"?></tr></td> <tr><td><?php echo "<input type='hidden' name='iduser' value='$id'>"?></td> <input value="Submit" type="submit" name="submit"> </div> <div class="right"> <?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $queryi= "SELECT * FROM `users` WHERE `username` = '$usernamecookie'"; $resulti = mysql_query($queryi) or die(mysql_error());; while($responsei = mysql_fetch_assoc($resulti)){ $newid = "{$responsei['id']}"; $imagevalue= "{$responsei['image']}"; } if ( $imagevalue == 'false'){ echo "<img src='http://www.url.com/profile/replace.png'>"; }else { echo "<img src='http://www.url.com/image/viewlarge.php?id=$newid&r=mt_rand(1,5000)' onmouseover='this.style.opacity=1;this.filters.alpha.opacity=100' onmouseout='this.style.opacity=0.4;this.filters.alpha.opacity=80' />"; } ?> </div> Quote Link to comment Share on other sites More sharing options...
contra10 Posted April 1, 2009 Author Share Posted April 1, 2009 can i somehow get rid of the cache Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.