File Management ColdFusion Help

ColdFusion provides facilities for ~orking with files on the server, as well for sending files from the client browser to the server for storage and processing. For the most part, this work is accomplished with the CFFILEtag. This tag enables you to upload files from the browser through <INPUT TYPE=FILE)form fields; to copy, move, delete.and rename existing files; and to change and read the content of text files. An additional tag, CFDIRECTORYal,lows the creation and-renaming of directories on the server as well as the generation of lists of files in a directory.

Understanding the CFFILE Tag

Before learning how to use the CFFILEtag in real applications, you need to take a quick look at the general structure of the tag. The CFFILEtag can take many attributes. However, one attribute is central to how the tag works: the ACTIONattribute. Not only does the ACTIONattribute determine what will .happen, it also determines which other attributes are valid arid can even affect the meaning of some attributes.

The CfFILE tag can take eight values for the ACTIONattribute:

UPLOAD  Uploads a file from a form to the server

MOVE  Moves a file to a new location

RENAME  Renames a file copy Makes a copy of a file in a new location
DELETE  Deletes a file

READ Reads the contents of a text file into a variable

WRITE  Writes content to a text file

APPEND  Adds content to a text file

READBlNARY Reads a binary file

In all cases, the files being affected reside on the ColdFusion server. ACTION=UPLOAisD ·asmall exception in that it involves uploading a file from the client system’s hard disk to a file on the server.
Throughout this chapter, you will be considering specific cases from the preceding list. In each case, you will review which attributes can be used with the specific action.

Uploading Files from a Form

In Chapter 15, “Forms,” we. purposely avoided discussion of upload.fields «INPUT TYPE-FILE». In this chapter, we will finally discuss how to use these fields to.upload files from a client system to the server.
There are two pieces to the puzzle: the form itself, and the ColdFusion template that receives the file from the form and performs some kind of action on it.

Creating a File Upload Form Field

First, let’s review the HTML used to create file upload form .fields.
A file upload form field is creating using the TYPE-FILE attribute of the INPUT tag. For instance, the following form creates a file upload field:

<FORM ACTION-‘upload.cfm’

This code produces a form that looks like the one

The FORM tag includes the ENCTYPE attribute set to multi part/form-data. This tells the server that the form’ includes a file upload as part of the data being sent and is an essential part of the FORM tag.
Users can either enter a full path and filename in the field, or can click the Browse button to choose a file from a file dialog box. After a file is chosen, the path and filename appear in the form field,

When the user submits the form, the file is sent along with the rest of the form data, D1L:chin the same way that a file is attached to an e-mail message. The script or applicanon receiving the form also can access the file and perform any relevant action on the file or its contents.

Acting on an Uploaded File

It is at this stage, after a file is uploaded with a form, that a Cold Fusion template can act on this file, using the CFFILE tag. As would be expected, the UPLOAD action of the CFFILE ~:l~is used to manipulate the uploaded file. ‘vI/henACTION=” UPLOAD” is used with the CFFILE tag, six other attributes are available ” use. as outhned in the following list: ,lCCEPT Specifies which types of files should be accepted for upload, effectively limit the range of files that can be uploaded. If used, the value of this field should be aI.. ‘MTT’i1-separated list of Etypes, such as image page, text/html.

ATTRIBUTES Specifies file attributes to be set on an uploaded file. 11-.-2 value of this field is a comma-separated list of attributes from the following list: ReadOn 1y, Temporary’ Archive, Hidden, System, and Norma 1. If this attribute is not used, then the original attributes of the uploaded file are retained. DESTINATION Specifies the directory on the ColdFusion server where the uploaded file should be saved. This is a required attribute. The attribute can also specify a new name for the file.

FILEFIELD Specifies the name of the form field being used to upload the file. This is a required attribute.

MODE Specifies the permissions that should be applied to a file when it is saved. This attribute is not applied in the Windows version of ColdFusion. In the Solaris version of Cold Fusion, possible values are numerical octal triplets that are valid with the Unix chmodcommand.

NAMECONFlICT Specifies how name conflicts between the uploaded file and existing files on the ColdFusion server should be handled. Possible values are ERROR(the file is not saved and an error message is returned), SKIP (the file is not saved and an error is not generated), OVERWRITE(the existing file is overwritten by the uploaded file if the names are the same), and MAKEUNIQUE(a new, unique file name will be created by the ColdFloJsion server for the uploaded file, and a variable called Fi 1e. ServerFi 1e will be created containing the name of the uploaded file). ERRORis the default value for this attribute.

Let’s consider a simple example. Your form contains a field named Up1oadFi 1e, which is a file upload field. The file being uploaded in this field should be saved in the directory temp and a new filename should automatically be assigned to the uploaded copy of the file. This is achieved with the following CFFILE tag:


The name of the resulting file can be found in the File . sever variable


<CFOUTPUT>Your file was uploaded and saved as #File.ServerFile#</CFOUTPUT>

Posted on November 14, 2015 in File Management

Share the Story

Back to Top
Share This