#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0

    How to rename file when uploading


    I am using the following code to upload and it works but I want it to rename the file before moving to folder and saving how do I do this?

    Tim
    PHP Code:
    $name $_POST['name'];
    $email $_POST['email'];
    $number $_POST['number'];
    $allowedExts = array("gif""jpeg""jpg""png");
    $extension end(explode("."$_FILES["file"]["name"]));
    if (((
    $_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/jpg")
    || (
    $_FILES["file"]["type"] == "image/png"))
    && (
    $_FILES["file"]["size"] < 20000000000000)
    && 
    in_array($extension$allowedExts))
      {
      if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Return Code: " $_FILES["file"]["error"] . "<br>";
        }
      else
        {
        

        if (
    file_exists("mom_photos/" $_FILES["file"]["name"]))
          {
          echo 
    $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          
    move_uploaded_file($_FILES["file"]["tmp_name"],
          
    "mom_photos/" $_FILES["file"]["name"]);
          echo 
    "Photo Added!<br><br>";
          
    $img $_FILES["file"]["name"];
          print 
    "<img src=\"mom_photos/$img\" width=200>";
          print 
    "<meta http-equiv=\"REFRESH\" content=\"0;url=?access=View Mom Photos\">";
          
    mysql_query("INSERT INTO momme (name, email, image, number)
    VALUES ('
    $name', '$email', '$img', '$number')");
          
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      } 
    Daily Deals from Kingston to Brockville and Cornwall Ontario. www.seawaydeals.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,466
    Rep Power
    594
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Alternatively you could actually just change the second parameter of move_uploaded_file so that you move the file to the name that you want.

    $_FILES["file"]["size"] < 20000000000000
    Is this really necessary? 20000000000000 bytes is 18 terabytes; I'm pretty sure it's safe to just implicitly assume that the file the user is uploading is smaller than 18TB.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    Sorry I have no idea how to do this

    Tim
    Daily Deals from Kingston to Brockville and Cornwall Ontario. www.seawaydeals.com
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You either rename the file with rename or you rename the target file in your line here:
    PHP Code:
    move_uploaded_file($_FILES["file"]["tmp_name"], 
          
    "mom_photos/" $_FILES["file"]["name"]); 
    What were your 900+ posts about if it wasn't PHP?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo