Use the CFILE tag to handle all interactions with files. CFILE is more complicated than other CFML tags because the required attributes depend on the value of the ACTION attribute. For example, if the ACTION is "WRITE" Cold Fusion expects the attributes associated with writing a text file.
Note: Many CFFILE attributes apply to specific actions. Refer to the ACTION Attributes table in this section for details about which attributes can be used with which ACTIONs.
This table shows the attributes you can use with which CFFILE ACTIONs.
The CFFILE tag has the following attributes for the upload action:
Whenever a file is being saved to the server there is a risk that another file may already exist with the same name. In the event of this occurrence there are number of actions you can take using the NAMECONFLICT attribute.
The CFFILE NAMECONFLICT attribute can have any one of the following settings:
The following example will create a unique file name if there is a name conflict when the file is upload:
<CFFILE ACTION="UPLOAD" FILEFIELD="FileContents" DESTINATION="C:\Web\Uploads\" NAMECONFLICT="MAKEUNIQUE">
After a file upload is completed, there is a set of status information about the upload that you can use. The status information includes a wide range of data about the file upload including variables such as the fileÆs name and the directory where it was saved.
The status information about a file upload is available as a set of parameters. File upload status parameters (listed below) use the "File" prefix (i.e. "File.ClientPath"). The file status parameters can be used anywhere other Cold Fusion parameters can be used. For more information, refer to the Cold Fusion User Guide.
The following file upload status parameters are available after an upload:
Tip: Use the File prefix to refer to these parameters (e.g. #File.FileExisted# ). Note: File status parameters are read-only. They are set to the results of the most recent CFFILE tag. (If two CFFILE tags execute, the results of the first tag are lost.)
The CFFILE tag can be used to move a file from one location on the server to another.
The CFFILE tag has the following attributes for the move action:
CFFILE Tag ACTION="MOVE" | ||
Attribute |
Description |
|
SOURCE |
Required. The file to move (with directory location). |
|
DESTINATION |
Required. The directory where the file will be moved to. |
The following example moves the file "KeyMemo.doc" file from the c:\files\upload\ directory to the c:\files\memo\ directory:
<CFFILE ACTION="MOVE" SOURCE="c:\files\upload\KeyMemo.doc" DESTINATION="c:\files\memo\">
The CFFILE tag can be used to rename a file that already exists on a server.
The CFFILE tag has the following attributes for the rename action:
CFFILE Tag ACTION="RENAME" | ||
Attribute |
Description |
|
SOURCE |
Required. The file to rename (with directory location). |
|
DESTINATION |
Required. The new name for the file (with directory location). |
The following example renames the file KeyMemo.doc to OldMemo.doc:
<CFFILE ACTION="MOVE" SOURCE="c:\files\memo\KeyMemo.doc" DESTINATION="c:\files\memo\OldMemo.doc">
The CFFILE tag can be used to copy a file from one directory to another on the server.
The CFFILE tag has the following attributes for the copy action:
CFFILE Tag ACTION="COPY" | ||
Attribute |
Description |
|
SOURCE |
Required. The file to copy (with directory location). |
|
DESTINATION |
Required. The directory where the copy of the file will be saved. |
The following example saves a copy of the KeyMemo.doc file in the c:\files\backup\ directory:
<CFFILE ACTION="COPY" SOURCE="c:\files\upload\KeyMemo.doc" DESTINATION="c:\files\backup\">
The CFFILE tag can be used to delete a file on the server.
The CFFILE tag has the following attributes for the delete action:
CFFILE Tag ACTION="DELETE" | ||
Attribute |
Description |
|
FILE |
Required. The file to delete (with directory location). |
The following example permanently deletes the specified file:
<CFFILE ACTION="DELETE" FILE="c:\files\upload\#Variables.DeleteFileName#">
You can use the CFFILE tag to read an existing text file. The file is read into a dynamic parameter which you can use anywhere in the template file, like any other dynamic parameter. For example, you could read a text file and then insert its contents into a database. Or you could read a text file and then use one of the find and replace functions to modify the contents.
The CFFILE tag has the following attributes for the read action:
The following example will create a variable named "Message" which will contain the contents of the file "update.txt:"
<CFFILE ACTION="READ" FILE="C:\Web\update.txt" VARIABLE="Message">
The variable "Message" could then be used in the template. For example, you could display the contents of the message.txt file in the final Web page:
<CFOUTPUT>#File.Message#</CFOUTPUT>
Tip: Cold Fusion supports a number of powerful functions for manipulating the contents of text files. (See the string functions in the Cold Fusion Language Reference for more information about how to use these functions.)
You can use the CFFILE tag to write a text file based on dynamic content. For example, you could create static HTML files or log actions in a text file.
The CFFILE tag has the following attributes for the write action:
CFFILE Tag ACTION="WRITE" | ||
Attribute |
Description |
|
FILE |
Required. The name of the file to be created (with directory location). |
|
OUTPUT |
Required. The content of the file to be created. |
The following example creates a file with the information a user entered into an HTML insert form:
<CFFILE ACTION="WRITE" FILE="C:\files\updates\#Form.UpdateTitle#.txt" OUTPUT="Created By: #Form.FullName# Date: #Form.Date# #Form.Content# ">
If the user submitted a form with where:
UpdateTitle="FieldWork" FullName="John Long" Date="10/1/96" Content="We had a wonderful time in Cambridgeport."
Cold Fusion would create a file named "FieldWork.txt" in the C:\files\updates\ directory. And the file would contain the text:
Created By: John Long Date: 10/1/96 We had a wonderful time in Cambridgeport.
The CFFILE tag can be used to append additional text to the end of an existing text file, for example when creating log files.
The CFFILE tag has the following attributes for the append action:
The following example will append the text string "But Davis Square was more fun." to the file FieldWork.txt which was created in the previous example:
<CFFILE ACTION="WRITE" DESTINATION="C:\files\updates\FieldWork.txt" OUTPUT="<B>But Davis Square was more fun.</B>">