So far, you have looked at how to establish a connection to an FfP server, but have not considered a larger issue: How to keep that connection open so that multiple file and directory operations can be performed.
By default, the CFFTP tag will establish the connection requested and then promptly disconnect it. This makes the act of opening a connection rather pointless. But you can resolve this problem by caching connections. A cached c-onnection is kept open and assigned a name, so that subsequent operations.jncluding the file and directory operations you will learn about later in ‘this chapter, can make use of the open connection.
To cache a connection when initially accessing an FfP server, add the CONNECTION attribute to the CFFTP tag, In the previous example in which you established an anonymous FIP connection, you could cache it using the following tag:
USERNAME=’ anonymous ,
Once established with the CONNECTION attribute in this way, future CFFl”P fags in the same template can use the connection name FtpConnection to access the specified server, as you will see later in the “Using the ACTION Attribute” section of this chapter.
Manipulating Files and Directories on the Server
When attempting file and directory operations using the CFFTPtag, a far richer set of attributes is available for use. These attributes are outlined in the following list:
ACTION Specifies the action to perform.
USERNAME Specifies the usemame to use in connecting to the server if the connection is not cached.
PASSWORD Specifies the password to use in connecting to the server if the connection is not cached.
NAME Specifies the query name for the results of a directory listing (ACTION),
SERVER Specifies the server to connect to if the connection is not cached.
TIMEOUT Specifies the timeout value in seconds to use when attempting the operation. The default is 30 seconds.
PORT Specifies the port to use for the operation. The default is port 21 for FfP operations.
CONNECTION Specifies the name of the FIP connection. This is used to access an existing
cached connection or to cache a new connection.
ASCIIEXTENSIONLIST A list of file extensions, separated by semicolons, which will automatically be downloaded using ASCII mode (as opposed to binary mode) .
TRANSFERMODE‘ to detect’ . By default, the list.
AGENTNAME The name of the application or entity conducting a file transfer.
FAILIFEXISTS Indicates whether the ACTION-‘GetFiie’ operation should fail if a local file already exists with the specified local filename for the transfer. Possible values are YES and NO,and the default value is YES.
DIRECTORY Specifies the directory to perform an action. on, and is required for ACTION Change ,ACTION-‘ Create ACTION
LOCALFILE Specifies the name of the file on the local system. Required for ACTION=’GetFile· and ACTION-‘PutFile’.
REMOTEFILE Specifies the name of the file on the FTP server. Required
Although this list-of attributes seems daunting, it really isn’t bad. You will start by taking a look at the ACTIONattribute, and then will consider the major actions most com monly used when working with an FfP server:
• Getting a file
• Putting a file
• Getting a directory listing
• Changing directories
Usingthe ACTION Attribute
The ACTIONattribute is the centerpiece of any CFFTPfile or directory operation. Possible file and directory operations are specified by the values of this attribute, as outlined.
When specifying any of the actions outlined in Table 24.3, if you have an already cached, open connection, then the SERVERU, SERNAMEa,nd PASSWORaDttributes are not required-only the CONNECTION attribute is required. For instance, with the cached connection called FtpConnection used earlier in this chapter, you could get the current directorv with
If the cached connection is not available, then adding SERVERU, SERNAMEand PASSWORD to the tag would enable a connection to the server to be established to perform the GetCurrentDiraction.
In all the following examples in this chapter, it is assumed that a cached-connection called FtpConnecti on exists, and that it should be used for all the operations.
Getting a File
Downloading a file from the current directory of the remote server to the local ColdFusion server involves using ACTION=”GetFil”. To do this, you need to specify two attributes: LOCALFILE and REMOTEFILE
for instance, to download the file foo . txt from the current directory on the FTP server to the c: \ temp directory on the local Cold Fusion server, you could use
If, in the process of downloading the file you want the new copy on the ColdFusion server to have the name newfoo. txt, you can change the value of the LOCALFILE attribute:
Putting a File
Putting a file is the reverse process of getting a file: It uploads a file from the local Cold Fusion server to the remote FTP server, and uses the LOCALFILEand REMOTEFILE attributes to specify the necessary information.
For instance, to upload the file c: \ temp\ foo. txt on the local system to the current directory on the remote FTP server, you would use the following tag.