Writes a text file on the server, based on dynamic content. You can create static HTML files from the content, or log actions in a text file.
<cffile action = "write" file = "full pathname
" output = "content
" addNewLine = "yes|no" attributes = "file attributes list
" charset = "character set option
" fixnewline = "yes|no" mode = "permission
">
See the History section of the main cffile tag page.
Attribute |
Req/Opt |
Default |
Description |
---|---|---|---|
action |
Required |
|
Type of file manipulation that the tag performs. |
file |
Required |
|
Pathname of the file to write. If not an absolute path (starting with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function. |
output |
Required |
|
Content of the file to be created. |
addNewLine |
Optional |
yes |
|
attributes |
Optional |
|
Applies to Windows. A comma-delimited list of attributes to set on the file. If omitted, the file's attributes are maintained. Each value must be specified explicitly. For example, if you specify attributes = "readOnly", all other attributes are overwritten.
|
charset |
Optional |
JVM default file character set |
The character encoding in which the file contents is encoded. The following list includes commonly used values:
For more information character encodings, see www.w3.org/International/O-charset.html. |
fixnewline |
Optional |
no |
|
mode |
Optional |
|
Applies only to UNIX and Linux. Permissions. Octal values of UNIX chmod command. Assigned to owner, group, and other, respectively; for example:
|
This example creates a file with information a user entered in 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 the following:
UpdateTitle = "FieldWork" FullName = "World B. Frueh" Date = "10/30/01" 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 following text:
Created By: World B. Frueh Date: 10/30/01 We had a wonderful time in Cambridgeport.
This example shows the use of the mode attribute for UNIX. It creates the file /tmp/foo with permissions 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>
This example uses the fixnewline attribute to changes embedded line-ending characters in xmlString, which is derived from xmlData, to operating-system specific line endings.
<cfxml variable="xmlData"> <docroot> <payload type="string">This is some plain text</payload> </docroot> </cfxml> <cfset xmlString = toString(xmlData)> <cfset key = createUUID()> <cfset encString=encrypt(xmlString, key)> <cffile action="write" addnewline="yes" file="C:\CFusionMX7\wwwroot\test\store.dat" output="#encString#" fixnewline="yes"> <cffile action="read" file="C:\CFusionMX7\wwwroot\test\store.dat" variable="retrievedString"> <cfset decString=decrypt(retrievedString, key)> <cfdump var="#decString#"> <cfset newXML = xmlParse(decString)> <cfdump var="#newXML#">
ColdFusion 8 supports using cffile to write an image, for example:
<!--- Create a new cfimage. ---> <cfset myImage=ImageNew("",200,200)> <!--- Draw a square on the image. ---> <cfset ImageDrawRect(myImage,10,10,100,100)> <!--- Use cffile to write the cfimage to a JPG. ---> <cffile action="write" output="#myImage#" file="c:\cfpix\square.jpg">