View comments | RSS feed
Contents > Developing ColdFusion MX Applications > Interacting with Remote Servers > Performing file operations with cfftp PreviousNext

Performing file operations with cfftp

The cfftp tag lets you perform tasks on remote servers using File Transfer Protocol (FTP). You can use cfftp to cache connections for batch file transfers when uploading or downloading files.

Note: To use cfftp, the Enable cfftp Tag option must be selected on the Tag Restrictions page of the Basic Security section of the ColdFusion MX Administrator Security tab.

For server/browser operations, use the cffile, cfcontent, and cfdirectory tags.

Using cfftp involves two major types of operations: connecting, and transferring files. The FTP protocol also provides commands for listing directories and performing other operations. For a complete list of attributes that support FTP operations and additional details on using the cfftp tag, see CFML Reference.

To open an FTP connection and retrieve a file listing:

  1. Create a ColdFusion page with the following content:
    <html>
    <head>
       <title>FTP Test</title>
    </head>
    
    <body>
    <h1>FTP Test</h1>
    <!--- Open ftp connection --->
    <cfftp connection="Myftp"
       server="MyServer"
       username="MyUserName"
       password="MyPassword"
       action="Open"
       stoponerror="Yes">
    
    <!--- Get the current directory name. --->
    <cfftp connection=Myftp
       action="GetCurrentDir"
       stoponerror="Yes">
    
    <!--- output directory name --->
    <cfoutput>
       The current directory is:  #cfftp.returnvalue#<p>
    </cfoutput>
    
    <!--- Get a listing of the directory. --->
    <cfftp connection=Myftp
       action="listdir"
       directory="#cfftp.returnvalue#"
       name="dirlist"
       stoponerror="Yes">
    <!--- Close the connection.--->
    <cfftp action="close" connection="Myftp">
    <p>Did the connection close successfully? 
       <cfoutput>#cfftp.succeeded#</cfoutput></p>
    
    <!--- output dirlist results --->
    <hr>
    <p>FTP Directory Listing:</p>
    
    <cftable query="dirlist" colheaders="yes" htmltable>
       <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="#DateFormat(lastmodified)#">
       <cfcol header="<B>IsDirectory</b>"
          TEXT="#isdirectory#">
    </cftable>
    
  2. Change MyServer to the name of a server for which you have FTP permission.
  3. Change MyUserName and MyPassword to a valid username and password.

    To establish an anonymous connection, enter "anonymous" as the username and an e-mail address (by convention) for the password.

  4. Save the file as ftp_connect.cfm in the myapps directory under your web_root and view it in the web browser.

Reviewing the code

The following table describes the code and its function:

Code

Description

<cfftp connection="Myftp"
   server="MyServer"
   username="MyUserName"
   password="MyPassword"
   action="Open"
   stoponerror="Yes">

Open an FTP connection to the MyServer server and log on as MyUserName. If an error occurs, stop processing and display an error. You can use this connection in other cfftp tags by specifying the Myftp connection.

<cfftp connection=Myftp
   action="GetCurrentDir"
   stoponerror="Yes">
<cfoutput>
   The current directory is:  #cfftp.returnvalue#<p>
</cfoutput>

Use the Myftp connection to get the name of the current directory; stop processing if an error occurs.

Display the current directory.

<cfftp connection=Myftp
   action="ListDir"
   directory="#cfftp.returnvalue#"
   name="dirlist"
   stoponerror="Yes">

Use the Myftp connection to get a directory listing. Use the value returned by the last cfftp call (the current directory of the connection) to specify the directory to list. Save the results in a variable named dirlist (a query object). Stop processing if there is an error.

<cfftp action="close" connection="Myftp">
<p>Did the connection close successfully? 
   <cfoutput>#cfftp.succeeded#</cfoutput></p>

Close the connection, and do not stop processing if the operation fails (because you can still use the results). Instead, display the value of the cfftp.succeeded variable, which is Yes if the connection is closed, and No if the operation failed.

<cftable query="dirlist"       colheaders="yes" htmltable>
  <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="#DateFormat(lastmodified)#">   <cfcol header="<B>IsDirectory</b>"
    TEXT="#isdirectory#"> </cftable>

Display a table with the results of the ListDir FTP command.

After you establish a connection with cfftp, you can reuse the connection to perform additional FTP operations until either you or the server closes the connection. When you access an already-active FTP connection, you do not need to re-specify the username, password, or server. In this case, make sure that when you use frames, only one frame uses the connection object.

Note: For a single simple FTP operation, such as GetFile or PutFile, you do not need to establish a connection. Specify all the necessary login information, including the server and any login and password, in the single cfftp request.


Contents > Developing ColdFusion MX Applications > Interacting with Remote Servers > Performing file operations with cfftp PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


home.com said on Aug 3, 2004 at 4:42 PM :
I would like to use your service and I will pleased if you could allow me to do so.
home.com said on Aug 3, 2004 at 4:43 PM :
I have no problem
CRJAngel said on Oct 26, 2004 at 7:42 AM :
who cares about opening and closing a connection and listing a directory?

Put something useful here like how to use Getfile to actually transfer a file to your hardrive.
sam504u said on Nov 15, 2004 at 12:31 PM :
I agree with you CRJAngel , heres what you are looking for.
For more info read:

http://www.macromedia.com/support/coldfusion/ts/documents/cfftp_sandbox.htm

It also has an example on how to getFile from the server using CFFTP>
Also the most imp thing is, When using cfftp in a template within a directory protected by sandbox security, the following error is reported:

Error Occurred While Processing Request
Security: The requested template has been denied access to
localhost:1024-.
The following is the internal exception message: access denied
(java.net.SocketPermission localhost:1024- listen,resolve)

ColdFusion cannot determine the line of the template that caused
this error. This is often caused by an error in the exception
handling subsystem.

In order to avod this we have to explicitly specify passive ="yes" , more about why and how ..... plz read the article it explains the technicality in more details.

If everything works and you could transfer the file successfully its good, else if you get an error something like this:

Security: The requested template has been denied access to /opt/coldfusionmx/runtime/servers/default/SERVER-INF/temp/wwwroot-tmp/downloaded_log.
The following is the internal exception message: access denied (java.io.FilePermission /opt/coldfusionmx/runtime/servers/default/SERVER-INF/temp/wwwroot-tmp/downloaded_file.txt write)

;then the problem is you need to specify the absolute path for the localfile name to be downloaded in the localFile attribute of the CFFTP tag.


Cheers !!!

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/remotes7.htm