Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    204
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Exclamation rename uploaded image name to time stamp

    How can I rename uploaded image name to time stamp

    here's my code

    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""root""root") or die(mysql_error()) ;
     
    mysql_select_db("mydatabase") or die(mysql_error()) ;

     
    //Writes the information to the database
     
    mysql_query("INSERT INTO `birthday` 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.";
     }
     
    ?>

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Hi,

    If you wanted to rename the file during the move to the target directory I believe that instead of setting the target to the name of the file like this

    PHP Code:
    $target "images/";
    $target $target basename$_FILES['photo']['name']); 
    you would just set the target to a date and time stamp like this

    PHP Code:
    $target "images/";
    $timestamp time();
    $target $target $timestamp
    you would then save this timestamp in the database table in order to correctly output the src url of the image on your site rather than saving and using the file name ($pic).

    Cheers,
    DouG.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •