PHP File upload - rename file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smilesinblues@hotpop.com

    #1

    PHP File upload - rename file

    Hi,
    I have to allow my visitors to upload image on my site.

    I am using the follwoing code to do that:

    $uploaddir = 'admin/';
    $uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
    move_uploaded_f ile($_FILES['userfile']['tmp_name'], $uploadfile);

    My problem is that I expect them to use same file name, like all of
    them will be uploading files like, comments.rtf. So if there is already
    one comments.rtf the file will get overwriten... so the challenge is to
    rename the files to username-filename.rtf

    can anybody here will take the pain to modify this code, I will be
    thankful, or please guide to me some tutorial where they are dealing
    with simple code and not with advanced classes.
    Thank you in advane

    Regards,
    Jaunty Edward

  • Geoff Berrow

    #2
    Re: PHP File upload - rename file

    I noticed that Message-ID:
    <1103007641.349 701.201220@f14g 2000cwb.googleg roups.com> from
    smilesinblues@h otpop.com contained the following:
    [color=blue]
    >
    >$uploaddir = 'admin/';
    >$uploadfile = $uploaddir . basename($_FILE S['userfile']['name']);
    >move_uploaded_ file($_FILES['userfile']['tmp_name'], $uploadfile);
    >
    >My problem is that I expect them to use same file name, like all of
    >them will be uploading files like, comments.rtf. So if there is already
    >one comments.rtf the file will get overwriten... so the challenge is to
    >rename the files to username-filename.rtf[/color]

    Is the username unique? If so it's just

    $uploadfile = $uploaddir.
    $username.basen ame($_FILES['userfile']['name']);

    Though you might like to add a character for neatness

    $uploadfile = $uploaddir .
    $username."-".basename($_FI LES['userfile']['name']);



    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • smilesinblues@hotpop.com

      #3
      Re: PHP File upload - rename file

      Hi,
      thank you so much Geoff Berrow its worked infact now I can use many
      ways to changing name of file.

      Thank you.

      Comment

      Working...