Best practice for renaming an uploaded file when using CFFILE
Posted on 2013-06-17
I am trying to fix some quirks in our CMS...when I upload a picture, it uses, CFFILE and then it passes the variable on to be added to the database. There are usually multiple sections to each page, so each 'section' is looped, id'd and processed. It looks like this, when it comes to the code that actually uploads the file and puts it on the server
<cfset "picture#text_seq#" = "#file.serverfile#">
1) ) The "new_pic#text_seq#" renders as new_pic4 (for example, the new pic for the fourth section).
2) the #s_ptag_serverpath1# is the directory that the file goes in.
3) The "picture#text_seq# (renders as "picture4" for the fourth section) is a variable that is then set to the file name, the variable is then sent on to be processed in the application, where the name is added to the database, for that graphic field.
The problem is that when a customer uploads a file the first time, everything works fine. If the customer uses the same graphic in a second section, the overwrite function prevents an error. The problem comes when the user/customer deletes the first or the second instance, which wipes it from the respective database field and then the file from the server - leaving the other instance still there in the database, but not on the server.
I need the second instance to be unique; I need to change the name of the file, after it is uploaded, but also need to set the value of the variable that passes the name on as well.
A file list is built of all the files in the directory, that the name can be checked against - so as to invoke the conditional statement of a rename function, I just don't know how to code it, or what the best way to do it is.
What is the best way to do this?