renaming a file on upload

Discussion in 'PHP' started by onlyican.com, Jan 22, 2006.

  1. #1
    Hey

    I am working on renaming a file on upload

    so far I have managed to put the new name infront of the old name

    this is my code

    $path1= "images/".$logged_in_user."_". $_FILES['ufile']['name'];
    
    move_uploaded_file($_FILES['ufile']['tmp_name'], $path1);
    
    PHP:
    how do I rename the file so it is just $logged_in_user

    And before you say, then i may over wright old images, good

    I want it so users can have only one image, if they upload a new image, it deletes the old one, instead of me having dead images floating about.

    Anybody
     
    onlyican.com, Jan 22, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    
    $_FILES['ufile']['name'] =$_FILES['ufile']['$logged_in_user'];
    
    Code (markup):
     
    Big 'G', Jan 22, 2006 IP
  3. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Don't work (or i am doing it wrong)


    $path1= "images/".$_FILES['ufile']['name'] =$_FILES['ufile'][$logged_in_user];

    move_uploaded_file($HTTP_POST_FILES['ufile']['tmp_name'], $path1);

    and I get this
    Warning: move_uploaded_file(images/): failed to open stream: Is a directory in /home/bla/public_html/site/folder/upload_picture.php on line 35

    Warning: move_uploaded_file(): Unable to move '/tmp/phpTHU0gj' to 'images/' in /home/bla/public_html/site/folder/upload_picture.php on line 35
    Your file has been uploaded
     
    onlyican.com, Jan 22, 2006 IP
  4. Important

    Important Peon

    Messages:
    87
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use:
    
    $file_ext = substr($_FILES['ufile']['name'], strrpos($_FILES['ufile']['name'], '.')+1); // get the file extension, like .gif, .jpg etc..
    $file_name = "images/" . $logged_in_user . '.' . $file_ext;
    move_uploaded_file($_FILES['ufile']['tmp_name'], $file_name); 
    
    PHP:
     
    Important, Jan 22, 2006 IP
  5. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you "Important"
    Perfect

    That is what i was planning to build (all i needed was a lil more knowledge)
     
    onlyican.com, Jan 22, 2006 IP