silverglade Posted February 22, 2012 Share Posted February 22, 2012 Hi, I have a "user profile" page and it has a profile image upload form. I have it so that the old profile image is deleted, and the new image is uploaded and the new image is echoed out using the name of the new file from the database. The problem is, when I click submit to add the new image, the old image still stays there and the new image does not show up. Only when I manually click refresh on my browser does the new image show up. That is going to be bad for my users. Please if anyone can help, I'd greatly appreciate it. Below is the code I am using to delete the old image, and code to upload the image as well, when the photo upload form is used. if(isset($_POST['photoUpload'])) { if(file_exists("images/".$currentUser.".jpg")){ unlink("images/".$currentUser.".jpg"); clearstatcache(); } //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } } //end if here //was else here //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name= $currentUser .'.'.$extension; //$image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname=$image_name; //"images/". // Connects to your Database mysql_connect("host", "user", "pass") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()) ; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], "images/".$newname); // update the photo name in the database $result = mysql_query("UPDATE users SET profilePhoto='$newname' WHERE username='$currentUser'") or die(mysql_error()); if (!$copied) { echo '<h1>Copy unsuccessful!</h1>'; $errors=1; } else{ $dir="images/"; echo "<p>Profile Picture Change Successful!</p>"; echo "<img src='{$dir}{$newname}' alt='{$newname}' height='200' width='200' />"; echo "<p></p>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/ Share on other sites More sharing options...
silverglade Posted February 22, 2012 Author Share Posted February 22, 2012 I think I can use this to force an image refresh, header('Content-type: image/jpeg'); readfile('images/'.$_GET['image']); but it gives me a "headers already sent" error because on the page I echo out a few things like the page header at the top from another php file. any help appreciated. thanks. ps: there is no extra whitespace at the top of my header file. Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319777 Share on other sites More sharing options...
silverglade Posted February 22, 2012 Author Share Posted February 22, 2012 I added this to the top of the script ob_start(); and this to the end of the script ob_end_flush(); and I get the error "The image http://mysite...blah blah" cannot be displayed because it contains errors" Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319783 Share on other sites More sharing options...
silverglade Posted February 22, 2012 Author Share Posted February 22, 2012 this does not force a refresh either. echo "<img src='{$dir}{$newname}?<? echo rand(1,3000); ?>' />"; Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319788 Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 It varies based on the user's cache settings. This is relevant though: http://www.phpfreaks.com/forums/index.php?topic=267298.0 Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319864 Share on other sites More sharing options...
silverglade Posted February 22, 2012 Author Share Posted February 22, 2012 ok thank you. I can't get anything on that linked page to work, I guess I will just have to deal with it . Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319924 Share on other sites More sharing options...
PFMaBiSmAd Posted February 22, 2012 Share Posted February 22, 2012 Because nothing you have tried worked, except refreshing the page, I'm going to guess that you have some code, such as your session_start/login check code, that contains an error in it that is preventing the code you did post from processing image the first time the page is submitted. Refreshing that page causes the form to be submitted a second time and the logic is now satisfied and actually processes the image the second time around. Post your whole form and form processing code, less any database credentials, that would be needed to duplicate the problem. echo "<img src='{$dir}{$newname}?<? echo rand(1,3000); ?>' />"; ^^^ That is invalid php. You are making and echoing a quoted string. You cannot put php code inside the string. You must concatenate any php code with the string and you wouldn't use php tags with it, just the dot . operator. I can only only guess that any further code you tried did not actually output a random number onto the end of the URL in the <img > tag. Did you look at the 'view source' of the resulting page to see if there was a random number? Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319931 Share on other sites More sharing options...
PFMaBiSmAd Posted February 22, 2012 Share Posted February 22, 2012 The code you posted at the start of this thread contains a number of problems - 1) You would never unlink the existing image until you know you have successfully uploaded and saved a new image. 2) You are not actually testing if the image uploaded without any errors ($_FILES['image']['name'] will be set for some of the possible upload errors.) 3) You are not actually using the result of any of the validation tests to prevent the following logic from running. 4) Your unlink code assumes that the existing image is a .jpg, but your logic allows jpg/png/gif images to be used. 5) If you are going to save the new image with a unique name, you would need to get the actual old image name from the database to use in your unlink statement. Doing this would address item #4 as well. 6) You should use move_uploaded_file to process the uploaded file. 7) You are executing the UPDATE query without knowing that the new image was successfully processed. Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319937 Share on other sites More sharing options...
silverglade Posted February 22, 2012 Author Share Posted February 22, 2012 WOW. lol. I just woke up a while ago. That stinks. I am going to have to work on all of that. Thanks for pointing it out. Will take me a while to digest what you have just said. Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319943 Share on other sites More sharing options...
silverglade Posted February 22, 2012 Author Share Posted February 22, 2012 Ok I was able to fix 2 and 4 on your list. Thank you very much for pointing out my not checking for png, gif or jpeg and not just jpg on my file delete. That was pretty serious. I will have to leave the other numbers on your list as I don't understand it totally. I am using a mix of my own code and tutorial code. So the page works, but it is not done totally correctly. I guess for now I will have to leave it as is except for fixing 2 and 4. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257496-replaced-profile-images-not-refreshing-on-page/#findComment-1319945 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.