cffile action = "read"

Description

Reads a text file on the server. The file is read into a dynamic, local parameter that you can use in the page. For example:

Note:   This action reads the file into a variable in the local Variables scope. It is not intended for use with large files, such as logs, because this can bring down the server.

Syntax

<cffile 
  action = "read" 
  file = "full_path_name"
  variable = "var_name"
  charset = "charset_option" > 

See also

cfdirectory

New in ColdFusion MX: this action attribute option supports the charset attribute.

Attributes

Attribute Req/Opt Default Description
action
Required
 
Type of file manipulation that the tag performs.
file
Required
 
Absolute pathname of file.
On Windows, use backward slashes; on UNIX, use forward slashes.
variable
Required
 
Name of variable to contain contents of text file.
charset
Optional
UTF-8
The Java character set name used for the file contents.
The following values are typically used:
  • UTF-8
  • ISO-8859-1
  • UTF-16
  • US-ASCII
  • UTF-16BE
  • UTF-16LE
For a list of character sets, see:
http://www.w3.org/International/
O-charset-lang.html

Usage

The following example creates a variable named Message for the contents of the file message.txt:

<cffile       action = "read" 
file = "c:\web\message.txt" 
variable = "Message">

The variable Message can be used in the page. For example, you could display the contents of the message.txt file in the final web page as follows:

<cfoutput>#Message#</cfoutput>

ColdFusion supports functions for manipulating the contents of text files. You can also use the variable that is created by a cffile action = "read" operation in the ArrayToList and ListToArray functions.

Comments