MySQL IS as file name

Discussion in 'PHP' started by rps111, Aug 14, 2007.

  1. #1
    Dear friends
    I want to develop a web form that will enable the user to enter the data in form fields and also allow the user to upload a file.

    The problem is, many users can upload the files with same name. So I would like that the uploaded files be renamed with as ID of the row (AUTO INCREMENT enabled) and stored in a folder named /files. A hyperlink to the file be stored in the MySQL DB.

    Please help me.

    Thanks in advance.
     
    rps111, Aug 14, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure which part you want help with? How to determine the ID? How to rename the file?

    Well, here's how you find out what the last inserted row's ID was...

    int mysql_insert_id ( [resource $link_identifier] )

    So, insert your stuff into the database first. Then retrieve the ID using the above function, then rename the file to match the ID.
     
    ecentricNick, Aug 14, 2007 IP
  3. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    But that's issuing 2 queries which is superfluous. A better solution would be to do the following:

    
    do 
    {
    	$szFilename = str_generate(8) . '.flv';
    }
    while (file_exists($szFilename));
    
    PHP:
    str_generate() is a function I use for generating a random number composed of both numbers and letters. This will then allow you to check if the randomly generated filename is unique, and then use one INSERT to insert all the data in to the specified table.
     
    Wildhoney, Aug 14, 2007 IP
  4. rps111

    rps111 Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually, I want that the ID in the MySQL DB should be the file name. e.g., if the ID of the entry in the MySQL DB is 123456, the file should be renamed to 123456.jpg/123456.pdf/123456.rar/123456.doc depending on the file type uploaded which may be jpg/pdf/rar/doc.

    I dont want the extension (jpg/pdf.....) to change but only the file name to change.
     
    rps111, Aug 16, 2007 IP
  5. Coder

    Coder Banned

    Messages:
    311
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Will you please post your sample code, you are using for uploading?
     
    Coder, Aug 16, 2007 IP