Renaming Files As They Are Uploaded (how CFFILE actually works)

8 March 2007 11:12 (EST)
When receiving a file uploaded from a client, you can actually save it and rename it all in one step rather than two steps as the documentation implies.

The documentation states that the "destination" attribute of the CFFILE tag specifies the "pathname of the directory in which to upload the file." This is misleading for two reasons:

The file has actually already been uploaded by the time your CFFILE tag is encountered. The file has automatically been saved in ColdFusion's temporary directory. Using the CFFILE tag with the action attribute set to "upload" is really just moving the file from one place to another. If no CFFILE tag is encountered, the file is simply deleted.
You can actually specify a file name in addition to a directory in the destination attribute. Rather than this...

<cffile action="upload" destination="/path/to/some/directory" ... />
<cffile action="rename" ... />

... you can just do this...

<cffile action="upload" destination="/path/to/some/directory/#createUUID()#.gif" ... />

This moves the uploaded file from ColdFuson's temporary directory into the specified directory, and renames it using a unique ID generated by ColdFusion, all in one step.

Discussion (4 comments)

Brian Brian: 18 November 2008 09:32 (EST)

Hey man, this was really handy... just used it at work on this project that's been giving me a headache!

Patrick Patrick: 5 October 2010 04:04 (EST)

This was very useful! Thanks for posting this!

Chet Chet: 26 October 07:52 (EST)

word for word from adobe 2004:
http://blogs.adobe.com/cantrell/archives/2004/07/renaming_files.html

Fred up Fred up: 25 January 03:19 (EST)

not useful at all...essentially renames every uploaded file to a .gif...useless...

Add your comment
*
*
*
*
*

Captcha Code Please enter the number on the left.
Sorry for asking you to do so.
Reload image