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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question renaming while uploading??

    i had a photo gallery for users where users can upload many photos..
    I am writing an script that renames the files while uploading as

    PHP Code:
    $new_filename=$user_id."_"."uniqueNO"
    $new_add=$new_filename.".jpg"
    rename($old_add,$new_add); 
    problem??
    >how to generate "uniqueNO" that never repeats?
    >is there any function??
    >this script changes any file extension to .jpg...is that good or else?
    Plz help in solving above questions....
    Thank U

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    uploaded files are given a unique name in the tmp directory reachable by $_FILES['nameofthefileinputformelement']['tmp_name']. I don't remember if it keeps the extension or not.

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I believe it does keep the extension, but I could be wrong. Also, for a unique id, you could write a functon, or use uniqid().
    "$question = ( to() ) ? be() : ~be();"

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    My win version of php assigna all files the *.tmp extension.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    I threw this into my friends script, which is what you are doing (I think) you can adjust the code obviously, though.

    PHP Code:
    // Grab extension from filename and create random id.
        
    $ext substr($file_namestrpos($file_name".")+1strlen($file_name));
        
    $new_file_name substr(md5(uniqid(microtime())), 08).".".$ext
    The random ID part is bigger then it really needs to be, but thats what he originally had, didn't want to mess with it.


  •  

    Posting Permissions

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