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 | ||||||||
---|---|---|---|---|---|---|---|---|
ATTRIBUTES | ISDIRECTORY | LASTMODIFIED | LENGTH | MODE | NAME | PATH | URL | |
1 | [empty string] | YES | {ts '2007-01-13 05:15:36'} | 0 | not currently supported | images | /images | ftp://localhost/images |
2 | [empty string] | NO | {ts '2007-01-13 05:15:36'} | 4 | not currently supported | index.html | /index.html | ftp://localhost/index.html |
query | ||||||||
---|---|---|---|---|---|---|---|---|
ATTRIBUTES | ISDIRECTORY | LASTMODIFIED | LENGTH | MODE | NAME | PATH | URL | |
1 | [empty string] | YES | {ts '2007-01-13 05:15:36'} | 0 | not currently supported | images | /images | ftp://localhost/images |
2 | [empty string] | NO | {ts '2007-01-13 05:15:36'} | 4 | not currently supported | index.html | /index.html | ftp://localhost/index.html |
3 | [empty string] | NO | {ts '2007-01-13 08:29:36'} | 6500 | not currently supported | ReadmeLater.htm | /ReadmeLater.htm | ftp://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: