Transfering Files with CFFTP
The CFFTP tag allows you to connect to an ftp server through Coldfusion. Having this ability comes in quite handy if you have to download or upload files from or two and ftp server on a regular basis.
Establishing a Connection
To establish a connection to a FTP server is actually quite simple, you need only the ftp address, username, password and a variable name to put into the connection attribute. You would then initiate an "open" action in the ftp tag The following code would attempt to connect to parishilton.com. Yes I tried to connect with the username and password and suprisingly enough it didn't work. If however it were tow work the connection would be held open and you would reference the open connection through your connection variable.<cfftp action = "open"
username = "parisHilton"
connection = "MyFTPConnection"
password = "password"
server = "ftp.parishilton.com"
stopOnError = "Yes">
username = "parisHilton"
connection = "MyFTPConnection"
password = "password"
server = "ftp.parishilton.com"
stopOnError = "Yes">
Listing Files
Now that you have an open connection to parishilton.com you may want to see what's actually on the ftp server. To do that you would initiate a list command. Normally if you were using a FTP client all of this would be done in the background for you. To do this with CFML you would initate a "listdir" action referencing your previously opened connection<cfftp action = "LISTDIR"
stopOnError = "Yes"
name = "Files"
directory = "/"
connection = "MyFTPConnection">
The name attribute is a variable name where the results of the LISTDIR command are saved. You can use the cfoutput tag to output
the results like you would for any query.
stopOnError = "Yes"
name = "Files"
directory = "/"
connection = "MyFTPConnection">
<cfoutput query="Files>
#name#<br/>
</cfoutput>
#name#<br/>
</cfoutput>
Closing the Connection
Once you are finished with working with your connection you should close it. You do so by initiating a "close" action.<cfftp action = "close"
connection = "MyFTPConnection"
stopOnError = "Yes">
connection = "MyFTPConnection"
stopOnError = "Yes">
Conclusion
As you can see using the CFFTP tag for a normal FTP server is quite simple, things do tend to get a bit more complicated when you have to connect to a secure server SFTP. Coldfusion 8 now provides the ability to connect to a SFTP server (though as of writing not using TLS-explicit).
A couple of other points to note.
- You don't need to first open a connection if all you are going to do is upload or download a single file using the getfile or putfile actions. e.g.
<cfftp action = "putFile"
username = "parisHilton"
password = "password"
server = "ftp.parishilton.com"
transferMode = "binary"
localFile = "C:\pictures\paris\paris_in_paris.jpg"
remoteFile = "/images/paris_in_paris.jpg"> - If you want to keep the connection open longer you can assign the connection to a session variable, but be careful with this and make sure you close the connection when you are finished.