Picture of Brian Love wearing black against a dark wall in Portland, OR.

Brian Love

FTP Facade Utility for ColdFusion

In a recent project at work we needed a simpler approach to using the FTP solution built into Adobe ColdFusion. A couple of the drawbacks to the current implementation of cfftp in ColdFusion are:

Our facade/utility does have a dependency on our Exception factory, which is stored in the application.exceptions application variable. If you would like to use this code you can replace these with your own exception handling or just plain cfthrow tags. Our utility class also uses a logger factory that follows the principles of the Java logging utility logf4j. You can read more about my simple logging utility. Lastly, the facade is programmed using CFML (tag-based) rather than the more succinct cfscript as our project required this.

Opening and Closing a Connection

Opening and closing a connection is as simple as calling either the open() or close() method. When opening a connection you must specify the connection parameters: server, username, and password. The class stores the connection parameters so you can also use the setters: setServer(), setUsername(), and setPassword(). Here is an example of opening the connection:

<<cfset variables.FTP_SERVER = 'ftp.remoteserver.com'>
<cfset variables.FTP\_USERNAME = 'username'>
<cfset variables.FTP\_PASSWORD = 'password'>
<cfset variables.FTP\_TIMEOUT = 1200>

<cfset ftpUtil = CreateObject("component", "FtpUtil")>
<cfset ftpUtil.open(server=variables.FTP\_SERVER, username=variables.FTP\_USERNAME, password=variables.FTP\_PASSWORD, timeout=variables.FTP\_TIMEOUT)>

Closing a connection is simple, just call the close method:

<cfset ftpUtil.close()>

If you want to reset the connection, which will close and reopen the connection, simply call the reset method:

<cfset ftpUtil.reset()>

File and Directory Operations

To get started, let’s look at the simple exists methods. You can call existsDirectory() or existsFile() to check if a file exists. You can use the getFile() method to download a file from the remote server. To upload files, use the putFile() and putDirectory() methods; and you can use the removeFile() and removeDirectory() methods to remove files and directories.

Note that the putDirectory() and deleteDirectory() methods are recursive. These will upload an entire local directory and all of it’s folders and files to the remote server, or delete all files and folders within a remote directory. Here are the method signatures:

You can also change directories and get the current directory (and URL of the current directory), and list the directory contents using the following methods:

Source Code

Download