Hello everyone, i have one question.
So, I've made avatar upload in my website and there is one problem.
My code is:

$path = "images/avatars/".$HTTP_POST_FILES['image']['name'];
rename($HTTP_POST_FILES['image']['tmp_name'], $path);


$query = mysql_query("UPDATE user SET imageloc='$path' WHERE username='$username'");

As you can see my image (avatar) is copied to: images/avatars/.
Now, when I'm changing my image (avatar) it's copying new image (avatar) to: image/avatar/.
but lieving the old one too... And it's just takes a lot of takes a lot of after a lot of images(avatars).

Is there are way to delete old image(avatar) when changing it to the new one?
Thank you. 🙂

    Or always write the same name for a user's avatar ... unless you want to offer them historical versions (for which I can see no reason).

    So, if I'm user #1234, my avatar is user_avatar_1234.png ... and when I upload a new one it's still "user_avatar_1234.png" ... there there's no "old file" because the new file overwrote the old one ....

      bradgrafelman;11030329 wrote:

      Retrieve the name of the old file and then use [man]unlink/man to remove it.

      So I've done that:

      $location = "images/avatars/$name";
      				$path = "images/avatars/".$HTTP_POST_FILES['image']['name'];
      				rename($HTTP_POST_FILES['image']['tmp_name'], $path);
      
      			$trinti = ("SELECT imageloc FROM user WHERE username='$username'");
      			unlink();
      
      			if ($trinti) {
      			$query = mysql_query("UPDATE user SET imageloc='$location' WHERE username='$username'");
      			echo "<script type='text/javascript'>    
      										window.location.reload('../index.php');    
      										window.close();
      										</script>   ";
      				}
      

      And I don't know if I've done everything right, but magically it's working 😮, but when I go to: images/avatars there is no avatars, but when I log out and log in It was saved. As I said i don't know if I've done everything right and if I did, then i need to fix it 'cause I need to see images/avatars/image.jpg, .png, .jpeg, .gif.
      Thanks. 🙂

      bradgrafelman;11030329 wrote:

      Retrieve the name of the old file and then use [man]unlink/man to remove it.

      I don't know how to do that, but I think it's a good idea too! Maybe you can tell me how to do that? 🙂
      Thanks. 🙂

        dalecosp;11030331 wrote:

        Or always write the same name for a user's avatar ... unless you want to offer them historical versions (for which I can see no reason).

        So, if I'm user #1234, my avatar is user_avatar_1234.png ... and when I upload a new one it's still "user_avatar_1234.png" ... there there's no "old file" because the new file overwrote the old one ....

        Sorry, I've made mistake by Quoting the wrong person. 🙂 I don't know how to do that, but I think it's a good idea too! Maybe you can tell me how to do that?
        Thanks. 🙂

          scdogas321;11030337 wrote:

          So I've done that:

          $location = "images/avatars/$name";
          				$path = "images/avatars/".$HTTP_POST_FILES['image']['name'];
          				rename($HTTP_POST_FILES['image']['tmp_name'], $path);
          
          			$trinti = ("SELECT imageloc FROM user WHERE username='$username'");
          			unlink();
          
          			if ($trinti) {
          			$query = mysql_query("UPDATE user SET imageloc='$location' WHERE username='$username'");
          			echo "<script type='text/javascript'>    
          										window.location.reload('../index.php');    
          										window.close();
          										</script>   ";
          				}
          

          And I don't know if I've done everything right, but magically it's working 😮, but when I go to: images/avatars there is no avatars, but when I log out and log in It was saved. As I said i don't know if I've done everything right and if I did, then i need to fix it 'cause I need to see images/avatars/image.jpg, .png, .jpeg, .gif.
          Thanks. 🙂

          I looked in my avatar folder today and if i've done right then this code not working :/

            That's because you don't do anything with $trinti, such as 1) execute it as a SQL query, 2) check to see if it succeeded, and 3) retrieve the result. Furthermore, you didn't give [man]unlink/man anything to... unlink.

            EDIT: Having said that, have you considered dalecosp's suggestion above? All you would need to do is simply use some unique value (such as the username or a user ID) for each user as that user's avatar filename.

              bradgrafelman;11030363 wrote:

              That's because you don't do anything with $trinti, such as 1) execute it as a SQL query, 2) check to see if it succeeded, and 3) retrieve the result. Furthermore, you didn't give [man]unlink/man anything to... unlink.

              EDIT: Having said that, have you considered dalecosp's suggestion above? All you would need to do is simply use some unique value (such as the username or a user ID) for each user as that user's avatar filename.

              I would be really appreciated if you fix my code to your suggestion or dalecosp. 🙂
              I'm not so good on php, sorry 🙁

              <?php
              
              	if ($_POST['submit']) {
              	require("./connect.php");
              
              $name = $_FILES['image']['name'];
              $tmp_name = $_FILE['image']['tmp_name'];
              
              	if ($name) {
              
              			$extensions = array('.png', '.gif', '.jpg', '.jpeg');  
              			$extension = strrchr($_FILES['image']['name'], '.');  
              
              			if(!in_array($extension, $extensions)){ 
              
              				echo "blgoas formatas";
              
              			}
              
              		else {
              		$file_size = $_FILE['image']['tmp_name'];
              		$limit_size=50000;
              
              			if($file_size >= $limit_size){
              			echo "Your file size is over limit";
              			}
              			else {
              
              
              			$location = "images/avatars/$name";
              			$path = "images/avatars/".$HTTP_POST_FILES['image']['name'];
              			rename($HTTP_POST_FILES['image']['tmp_name'], $path);
              
              
              			$query = mysql_query("UPDATE user SET imageloc='$location' WHERE username='$username'");
              			echo "<script type='text/javascript'>    
              										window.location.reload('../index.php');    
              										window.close();
              										</script>   ";
              
              
              			}
              		}
              	}
              	else
              		echo "J&#363;s turite pasirinkti nuotrauk&#261;";
              
              
              mysql_close();
              }
              
              echo "<form action='redaguoti.php' method='post' enctype='multipart/form-data'>
              
              			File:
              			<input type='file' name='image'> <input type='submit' name='submit' value='Upload'>
              			</form>";			
              
              ?>
              

              And by the way, sorry for other language used in my code, it's my language 🙂.
              Thanks

                Write a Reply...