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

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:

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