cfftp - Cached Connections

In the previous lesson, we covered some basic FTP actions using ColdFusion's cfftp tag. By default, ColdFusion opened the FTP connection for us, then closed it when the template finished executing. Therefore, each one of those actions required a new connection to be created.

This is fine if you only want to perform one action. But what if you want to perform multiple actions? Say you want to list a directory, copy a file, then list the directory again. Wouldn't it be more efficient to open the connection once, perform all tasks, then close the connection once everything has been done?

You need to cache your connection!

The <cfftp> tag's action attribute accepts both open and close as possible values. Using action="open" opens the connection, action="close" closes it.

When you open the connection, you name it. That way, you can use this connection again and again simply by referring to its name.

Depending on the contents of the directory, the above code might result in something like this:

query
  ATTRIBUTESISDIRECTORYLASTMODIFIEDLENGTHMODENAMEPATHURL
1[empty string]YES{ts '2007-01-13 05:15:36'}0not currently supportedimages/imagesftp://localhost/images
2[empty string]NO{ts '2007-01-13 05:15:36'}4not currently supportedindex.html/index.htmlftp://localhost/index.html
query
  ATTRIBUTESISDIRECTORYLASTMODIFIEDLENGTHMODENAMEPATHURL
1[empty string]YES{ts '2007-01-13 05:15:36'}0not currently supportedimages/imagesftp://localhost/images
2[empty string]NO{ts '2007-01-13 05:15:36'}4not currently supportedindex.html/index.htmlftp://localhost/index.html
3[empty string]NO{ts '2007-01-13 08:29:36'}6500not currently supportedReadmeLater.htm/ReadmeLater.htmftp://localhost/ReadmeLater.htm

FTP over Multiple Pages

If your application requires the user to perform FTP operations across multiple pages, you can add the connection to the session scope by creating it as as a session variable. For example: