Jump to content

replaced profile images not refreshing on page


silverglade
 Share

Recommended Posts

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>";

				}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.