Php upload that renames the files

Discussion in 'PHP' started by Phunk, Mar 28, 2006.

  1. #1
    I have a file uploader script and i'm wondering how to get it to rename the file for me eg. first file is renamed to 0001 then 0002 and so on.
     
    Phunk, Mar 28, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    T0PS3O, Mar 28, 2006 IP
  3. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Actually you can just name the uploaded file before writing it out. Writing it out and then copying it to a new name creates avoidable overhead. Plus copy leaves the old behind and would require a call to unlink later.

    Anywho, when you write the file out, just create any name you want. You want the files numbered. However, we need to know what number to start at each time the script is called. If we don't know, we will always start at 0001 and overwrite the previous uploads.

    So, where are you storing the files? I'm asuming the file system. So you need to store a temp file with the current number to assign that is readable and writable to update. Or, do some sort of directory read, read the number in the file name of all the uploaded files that exist, then go one higher. A array function could probably do that.
     
    noppid, Mar 28, 2006 IP
  4. Web1.in

    Web1.in Peon

    Messages:
    99
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can play around here...

    copy ($_FILES['file_input']['tmp_name'], $upload_path.$file_name);

    you can specify the names for $upload_path (it will be folder where file will be uploaded) and $file_name, the name of the file you want...

    I guess it should work :)

    Deep
     
    Web1.in, Mar 28, 2006 IP
  5. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
  6. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #6
    noppid, Mar 28, 2006 IP
  7. wwm

    wwm Peon

    Messages:
    308
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    wwm, Mar 29, 2006 IP