cffile action = "write"

Description

Writes a text file based on dynamic content. For example, you could create static HTML files from this content, or log actions in a text file.

Category

File management tags

Syntax

<cffile action = "write"
  file = "full_path_name"
  output = "content"
  mode = "permission"
  addNewLine = "Yes" or "No"
  attributes = "file_attributes"> 

See also

cfdirectory

Attributes

Attribute
Description
file
Required. The full pathname of the file to be created.
output
Required. The content of the file to be created.
mode
Optional. Defines permissions for a file on Solaris or HP-UX. Ignored on Windows. Valid entries correspond to the octal values (not symbolic) of the Unix chmod command. Permissions are assigned for owner, group, and other, respectively.
For example:
  • mode = "644"    Assigns read/write permissions for the owner, and read permissions for the group and other.
  • mode = "666"    Assigns read/write permissions for owner, group, and other.
  • mode = "777"    Assigns read, write, and execute permissions for all.
addNewLine
Optional. Yes or No. If Yes, a new line character is appended to the text that is written to the file. If No, no new line character is appended to the text. The default value is Yes.
attributes
Optional. A comma-delimited list of file attributes to be set on the file being written. The following file attributes are supported:
  • readOnly
  • temporary
  • archive
  • hidden
  • system
  • normal
If attributes is not used, the file's attributes are maintained. If normal is specified with any other attributes, normal is overridden by whatever other attribute is specified.
Individual attributes must be specified explicitly. For example, if you specify only the readOnly attribute, all other existing attributes are overwritten.

Example

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:

UpdateTitle = "FieldWork"
FullName = "World B. Frueh" 
Date = "10/30/98" 
Content = "We had a wonderful time in Cambridgeport." 

ColdFusion would create a file named FieldWork.txt in the c:\files\updates\ directory and the file would contain the text:

Created By: World B. Frueh
Date: 10/30/98 
We had a wonderful time in Cambridgeport.

This following examples show the use of the mode attribute for UNIX. The first, creates the file /tmp/foo with permissions defined as rw-r-r-- (owner = read/write, group = read, other = read).

<cffile action = "write"
  file = "/tmp/foo" 
  mode = 644>

This example appends to the file and sets permissions to read/write (rw) for all.

<cffile action = "append"
  destination = "/home/tomj/testing.txt" 
  mode = 666 
  output = "Is this a test?">

This example uploads a file and gives it the permissions owner/group/other = read/write/execute).

cffile action = "upload"
  fileField = "fieldname" 
  destination = "/tmp/program.exe" 
  mode = 777>