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.
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.
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.
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.