Blog Author: Doug Hughes

Doug Hughes is a veteran programmer who founded Alagad after briefly flirting with a career in computer animation. Doug has a strong record of achievement with large, complex web applications. His ability to learn and apply new technologies and techniques has helped Doug become one of the web application development industry’s most highly regarded experts.

Doug’s current focus at Alagad is leading an application development team dedicated to helping organizations improve operational efficiency and performance. This commitment to client success has helped Alagad achieve record growth. A frequent speaker at industry conferences and user groups, Doug also regularly contributes to several well-known open source projects. In addition, his articles have been featured in numerous technical publications.

Categories
Blog Archives
Blog RSS Feed

RSS FeedSubscribe to our RSS feed or Atom feed.

The Alagad Technical Team Blog

The Best Way To Create Temporary Files in ColdFusion

Published By: Doug Hughes on Nov 24, 2008 at 07:15 AM
Categories:

This morning my day was kicked off with a warning from OS X that I had next to no space left on my hard drive and that I'd better clean stuff up pronto.  Luckily, I knew exactly what to remove and I took care of it.  However, while cleaning up files I stumbled across a set of files in my home directory with names like "c/\temp\924A8A06-FE9F-462A-F4BDCA63C95797CE".  Now that's an odd name for a file on a Mac!.  I opened one of these files to see a barcode image and realized what was going on.

One of my client's developers had been working on creating these barcode images and must have hard coded the creation of the images to their "c:\temp" directory on Windows.  My experience highlights one reason why this might not be the best way to create temp files: The directory might not exist!  Or, it's conceivable that the non-standard c:\temp directory might not exist.  Or that ColdFusion might not have rights to write to that directory.  Or the application might be running on a non-Windows system like OS X, Linux or Unix.

So, with that in mind, what is the best way to create a temporary file in ColdFusion?  This is a fairly easy problem to solve with two built in ColdFusion functions, GetTempDirectory and GetTempFile.

The GetTempDirectory function simply returns the path to a temporary directory that exists and ColdFusion can write to.

The GetTempFile creates a file in a specified path with a prefix. 

Using these two functions together you can safely create a temporary file.  This is the example from the ColdFusion documentation:

<h3>GetTempFile Example</h3>
<p>The temporary directory for this ColdFusion Server is
    <cfoutput>#GetTempDirectory()#</cfoutput>.</p>
<p>We have created a temporary file called:
<cfoutput>#GetTempFile(GetTempDirectory(),"testFile")#</cfoutput></p>

One other point related to this, no mater what platform you're running on, you should always use a front slash as your path separator and not a back slash.  Java will automatically translate this to the correct separator for the platform your application runs on.

2 responses to “The Best Way To Create Temporary Files in ColdFusion”

I have actually moved away from using GetTempDirectory() as it has randomly stopped working on a machine that I used to use (in production). One day, GetTempDirectory() just started returning empty string as its path on a production server. We rebooted and it worked again. Then, a few months later, blam! It started doing it again. No one could figure out what was going wrong (not even people at Adobe). Since this experience, I have started using site-local temp directories per project when needed. Then, I usually just have some sort of task that deletes them (or maybe I delete them before I create a new one, etc.).

Not sure this has happened to anyone else, but just my experience.

Actually Java will automatically convert both forward and back-slashes as well as removing extraneous duplicated slashes...

<cfset objFile = CreateObject("java","java.io.File").init("c:/some\file/\with//bad\\slashes") />

<cfoutput>#objFile.getCanonicalPath()#</cfouptut>

Although in my experience getCanonicalPath() specifically will throw an error if the file doesn't exist.

Leave a Reply