I am modifying a commonly used PHP/MySQL system (DaDaBik) in which pathnames to uploaded files are stored in the database while the actual files are in table-specific directories. (A user may or may not choose to upload a file for the record he is adding.) Currently, there is no deletion of files when records are deleted, and the way DaDaBik is written, the following may be the easiest way to achieve this objective.
I want to associate the filenames with records by prefixing primary key values to them. So, a file named abc.jpg will become 123_abc.jpg after uploading, 123 being the primary key value of the record. Deleting record 123 would thus just involve looking for any file with prefix 123_.
The question is how do I get the primary key value for a record BEING inserted? (The primary key may or may not be of auto_increment type.) That way I can write the block of code to rename the uploaded file within the PHP function that has the MySQL insert statement.
Perhaps, this is not possible. The other option then is to let the record be inserted successfully, get the primary key value, check which file was uploaded (how?) and then copy (for adding the prefix) and unlink (the original upload).