Rename the image if already exists on sql & php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bala Kumaran
    New Member
    • Jan 2013
    • 30

    Rename the image if already exists on sql & php

    Hello Experts,

    I've added a user data insertion php script form in my web page. If the photo is already exists, I couldnot renaming the photo name by the following php script.

    My data inserting html form is here...

    Code:
     <form enctype="multipart/form-data" action="add.php" method="POST"> 
     Name: <input type="text" name="name"><br> 
     E-mail: <input type="text" name = "email"><br> 
     Phone: <input type="text" name = "phone"><br> 
     Photo: <input type="file" name="photo"><br> 
     <input type="submit" value="Add"> 
     </form>

    *************** **add.php****** *************

    Code:
    <?php 
     
     //This is the directory where images will be saved 
     $target = "images/"; 
     $target = $target . basename( $_FILES['photo']['name']); 
     
     //This gets all the other information from the form 
     $name=$_POST['name']; 
     $email=$_POST['email']; 
     $phone=$_POST['phone']; 
     $pic=($_FILES['photo']['name']); 
     
     // Connects to your Database 
     mysql_connect("localhost", "user", "pass") or die(mysql_error()) ; 
     mysql_select_db("db_name") or die(mysql_error()) ; 
     
     //Writes the information to the database 
     mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 
     
       //Writes the information to the database 
     mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 
     
     
     //Writes the photo to the server 
     if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
     { 
     
     //Tells you if its all ok 
     echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
     } 
     else { 
     
     //Gives and error if its not 
     echo "Sorry, there was a problem uploading your file."; 
     } 
     ?>
    SQL tables are here...

    Code:
    CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo VARCHAR(30))
    How to I rename the photo if already exists.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    i think the docs says that move_uploaded_f ile will overwrite it, so why would you rename it?
    (see: www.php.net/move_uploaded_file)

    if you want to check if it already there, before overwriting it, use file_exists
    (see: http://us2.php.net/function.file-exists)

    Comment

    • Bala Kumaran
      New Member
      • Jan 2013
      • 30

      #3
      -Thanks Luuk

      But, I asked the above question also to add "profile picture" of users in my website. So it is very important to rename the photo if the selected photos name already existed.

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        so in entered the second part in my previous response:
        "if you want to check if it already there, before overwriting it, use file_exists
        (see: http://us2.php.net/function.file-exists) "

        Which part of that did not answer the question, and why? ;-)

        Comment

        Working...