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="" ... />

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.

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

  1. Dave Carabetta says:

    Christian, I’m a little confused as to why you ever used two steps to begin with? I’ve used this for a long time, and it has always been one line for me:[cffileaction=”upload”nameconflict=”makeunique”destination=”/my/path”filefield=”Form.uploadFile”charset=”utf-8″/]It will use the value in the filefield attribute as the file name. No “rename” call and no “workaround” file name to the end of the destination attribute. Am I missing something? However, I do agree that the “upload” name for the action attribute is mis-leading, as the file has already been uploaded to ColdFusion’s temp directory by that point.

  2. Dave Carabetta says:

    PS — your server time is a bit off. I posted my comment at 4:53 pm EDT, and it came up as 5:37 pm (which freaked me out for a second because I would have been late for a meeting!!)

  3. Dave, that’s a good question. I should have provided more context. This topic came up in a conversation on the cfguru mailing list about Mac users uploading files with question marks in the names (don’t ask me why anyone would put a question mark in a file name, but apparently people do). The author of the original post was unable to work with the file because he found that the ColdFusion server would throw an error when attempting to reference a file with a question mark in it. Additionally, he was unable to allow HTTP requests to the file since, obviously, a question mark has special meaning in the HTTP protocol. The solution was to rename the file something other than the original file name, and it had to be dome with the first CFFILE operation since he could not reference it by its name. Judging from this guy’s experience, it seems to me that it might always make sense to rename uploaded files.PS: Sorry about the time. I’ll have a look.

  4. Dave Carabetta says:

    Ahhh, I see what you’re saying now. Yeah, his experience is definitely an eye-opener! Just when you think you have all your exception scenarios covered, another crops up!!

  5. Dick says:

    This is interesting. I hadn’t realised that the form submit actually uploads the file and the CFFILE just moves it.That begs a question:How long is the file retained in the temporary directory? I’m working on a requirement which means that the file is submitted, therefore uploaded, by one form but not CFFILE’ed until later in the workflow should another condition exist. How long is the file going to exist in the temporary directory? Minutes, hours, days? How can you force a cleanup?

  6. MB says:

    I was trying to do this exact procedure on one of my sites. Thank goodness I found this site! Thank you, thank you, thank you!

  7. Christian, I tried your code as a solution to a CFFILE problem I’ve been encountering with no luck. Perhaps you could help resolve this problem. I have a form that uploads 2 images. I tested this locally with CF 4.5 and it worked fine. Now that it is up on the server, CF 6.1, the path to CF’s temp file is inserted rather than the image file name. My hosting company is as perplexed as I am. Your create UUID gives the same result. Any suggestions? Thanks!

  8. Brad Fetter says:

    Brenda,You need to make sure your form is set to enctype=”multipart/form-data”Cheers,Brad

  9. Tony says:

    Hi, i would like to know if it’s possible, in your way, to do something like that :[cffile action=”UPLOAD”filefield=”FORM.Logo”destination=”d:\inetpub\wwwroot\MyFolder\#LCase(CFFILE.ServerFile)#”nameconflict=”makeunique”accept=”image/jpg,image/pjpeg,image/gif,image/bmp,image/tiff”]The previous code don’t work because element SERVERFILE is undefined in CFFILE. There is a way to do it like that, without using the two operations (upload and rename) ?Thanks !

  10. Christian,Good post, but, I might point out that this only works if you’re only accepting one file type, for example .gif, as you do above.When ColdFusion uploads the file, it is a *.tmp file, so unless you’re 100% sure that it’s a GIF file, you can’t dynamically rename it.i.e. If you’re building a document manager and you’re accepting Word, Excel, and PDF documents, they are all uploaded as *.tmp, so you have no way of knowing what file type they are until after they are uploaded, when you could attempt to handle it based on the MIME type, but that would be a bit kludgey. So, unless you’re only accepting one file type, you’ll still have to pull the old rename trick.One alternative to this is if you’re capturing the MIME type in the database, you could write a wrapper that would use cfcontent and cfheader to return the document using the MIME type and then it wouldn’t matter if the extension was *.tmp.

  11. Lightsabre says:

    I’ve had to build an upload for zip files only. I put validation for .zip on the client using JS, in the accept attribute in cffile, and using a script similar to Chrisitian’s.destination=”#ExpandPath(‘files/’& createUUID() &’.zip’)#”This way even if someone were to upload a non-zip, it’d save as a zip.

  12. Gordon Cantor says:

    I have developed a document upload (CFFILE) and search application in CF. It will allow the upload of multiple file formats, but not zip files. The upload form also collects some db data regarding the file being uploaded.The user wants me to allow the upload of zip files to avoid having to upload each file individually. I have been resisting this request on the following basis:1. Virus Protection – in actuality, this is not a big problem becuase our servers keep very current with virus protection.2. Weakened search capabilities – with multiple files clumped together as one while uploading, the searchable database information collected while uploading would be for the zip file, not for the hidden files it contains.**Any thoughts or suggestions regarding zip file uploads will be appreciated.

  13. Marc Ehrenkrantz says:

    filename: PrePatchVoice.xls uploads fine using cffilefilename: Pre Patch Voice.xls does notIs there a way to upload filenames with spaces?

  14. chief says:

    Thank you very much for this tip. I am somewhat new to CFML and working on a CMS for a client. I wanted to rename files on upload because I know I can’t trust the client to name files appropriately (go figure).Macromedia should really include this in it’s documentation for CFML because it is really necessary when you want to make sure files are named something non-random but distinctive.