Adobe ColdFusion 8

About file management

ColdFusion lets you access and manage the files and directories on your ColdFusion server. The cffile tag has several attributes for moving, copying, deleting, and renaming files. You use the cfdirectory tag to list, create, delete, and rename directories. The cfcontent tag lets you define the MIME (Multipurpose Internet Mail Extensions) content type that returns to the web browser.

Using cffile

You can use the cffile tag to work with files on the server in several ways:

  • Upload files from a client to the web server using an HTML form
  • Move, rename, copy, or delete files on the server
  • Read, write, or append to text files on the server

You use the action attribute to specify any of the following file actions: upload, move, rename, copy, delete, read, readBinary, write, and append. The required attributes depend on the action specified. For example, if action="write", ColdFusion expects the attributes associated with writing a text file.

Note: Consider the security and logical structure of directories on the server before allowing users access to them. You can disable the cffile tag in the ColdFusion Administrator. Also, to access files that are not located on the local ColdFusion system, ColdFusion services must run using an account with permission to access the remote files and directories.

Uploading files

File uploading requires that you create two files:

  • An HTML form to specify file upload information
  • An action page containing the file upload code

The following procedures describe how to create these files.

Create an HTML file to specify file upload information

  1. Create a ColdFusion page with the following content:
    <head><title>Specify File to Upload</title></head>
    <body>
    <h2>Specify File to Upload</h2>
    <!--- the action attribute is the name of the action page --->
    <form action="uploadfileaction.cfm" 
            enctype="multipart/form-data" 
            method="post">
        <p>Enter the complete path and filename of the file to upload:
        <input type="file"
                name="FiletoUpload"
                size="45">
        </p>
        <input     type="submit"
                value="Upload">
    </form>
    </body>
    

  2. Save the file as uploadfileform.cfm in the myapps directory under your web_root and view it in the browser.

Note: The form will not work until you write an action page for it (see the next procedure).

Reviewing the code

The following table describes the code and its function:

Code

Description

<form action="uploadfileaction.cfm" enctype="multipart/form-data" method="post">

Create a form that contains file selection fields for upload by the user. The action attribute value specifies the ColdFusion template that will process the submitted form. The enctype attribute value tells the server that the form submission contains an uploaded file. The method attribute is set to post to submit a ColdFusion form.

<input type="file" name="FiletoUpload" size="45">

Allow the user to specify the file to upload. The file type instructs the browser to prepare to read and transmit a file from the user's system to your server. It automatically includes a Browse button to let the user look for the file instead of manually entering the entire path and filename.

The user can enter a file path or browse the system and select a file to send.

  1. Create a ColdFusion page with the following content:
    <html>
    <head> <title>Upload File</title> </head>
    <body>
    <h2>Upload File</h2>
    
    <cffile action="upload"
            destination="c:\temp\"
            nameConflict="overwrite"
            fileField="Form.FiletoUpload">
            
    <cfoutput>
    You uploaded #cffile.ClientFileName#.#cffile.ClientFileExt#
                successfully to #cffile.ServerDirectory#.
    </cfoutput>
    
    </body>
    </html>
    

  2. Change the following line to point to an appropriate location on your server:
    destination="c:\temp\"

Note: This directory must exist on the server.

  1. Save the file as uploadfileaction.cfm in the myapps directory under your web_root.
  2. View uploadfileform.cfm in the browser, enter a file to upload, and submit the form.

    The file you specified uploads.

Reviewing the code

The following table describes the code and its function:

Code

Description

<cffile action="upload"

Output the name and location of the uploaded file on the client machine.

destination="c:\temp\"

Specify the destination of the file.

nameConflict="overwrite"

If the file already exists, overwrite it.

fileField="Form.FiletoUpload">

Specify the name of the file to upload. Do not enclose the variable in number signs.

You uploaded #cffile.ClientFileName#.#cffile. ClientFileExt# successfully to #cffile.ServerDirectory#.

Inform the user of the file that was uploaded and its destination. For information on scope variables, see Evaluating the results of a file upload.

Note: This example performs no error checking and does not incorporate any security measures. Before deploying an application that performs file uploads, ensure that you incorporate both error handling and security. For more information, see Securing Applications and Handling Errors.