After you establish a connection with cfftp
, you can reuse it to perform additional FTP operations. To do this, you use the connection
attribute to define and name an FTP connection object that stores information about the connection. Any FTP operations that use the same connection
name automatically use the information stored in the connection object. This helps save connection time and improves file transfer performance.
To keep the connection open throughout a session or longer, you can use a session or application variable as the connection name. However, if you do this, you must specify the full variable name, with the close
action, when you are finished. Keeping a connection open prevents others from using the FTP server; so close a connection as soon as possible.
Changes to a cached connection, such as changing retryCount
or timeout
values, might require reestablishing the connection.
The following example opens a connection and gets a file listing showing file or directory name, path, URL, length, and modification date.
<p>Open a connection
<cfftp connection = "myConnection" username = "myUserName" password = "myUserName@allaire.com" server = "ftp.allaire.com" action = "open" stopOnError = "Yes"> <p>Did it succeed? <cfoutput>#cfftp.succeeded#</cfoutput> <cfftp connection = "myConnection" action = "LISTDIR" stopOnError = "Yes" name = "ListDirs" directory = "/"> <p>FTP Directory Listing:<br> <cftable query = "ListDirs" HTMLTable = "Yes" colHeaders = "Yes"> <cfcol header = "<b>Name</b>" text = "#name#"> <cfcol header = "<b>Path</b>" text = "#path#"> <cfcol header = "<b>URL</b>" text = "#url#"> <cfcol header = "<b>Length</b>" text = "#length#"> <cfcol header = "<b>LastModified</b>" text = "Date(Format#lastmodified#)"> <cfcol header = "<b>IsDirectory</b>" text = "#isdirectory#"> </cftable> <p>Close the connection: <cfftp connection = "myConnection" action = "close" stopOnError = "Yes"> <p>Did it succeed? <cfoutput>#cfftp.succeeded#</cfoutput>