<cffile>

Handles all common interactions with files.

The attributes you use with cffile depend on the value of the action attribute.

For example, if the action = "write", use the attributes associated with writing a text file.

This tag may have a body.

This tag is also supported within <cfscript>

<cffile storeacl=object action=info|move|rename|copy|delete|read|readBinary|write|append|upload|uploadAll|touch serverpassword=string source=string attributes=string destination=string mode=string filefield=string charset=string nameconflict=string accept=string strict=boolean createpath=boolean file=string variable=string output=any cachedwithin=object addnewline=boolean fixnewline=boolean result=string allowedextensions=any blockedextensions=any ><!--- body --->[</cffile>]
Attribute Description
storeacl
object, optional

Access Control List (ACL) to use for new files. Can be a either a string ("authenticated-read", "private", "public-read", or "public-read-write"), or an array of structs where each struct represents a permission or grant.

Example: [{email="xxx@yyy.com", permission="full_control"}, {group="all", permission="read"}]

Defaults to the value specified in the application's this.s3.acl, which is "public" if unspecified.

Alias: acl

action
string, required

Type of file manipulation that the tag performs.

Note: action upload return the status of the upload operation as a struct after the file upload

serverpassword
string, optional

allow you to access filesystem, also when access is denied for your context

source
string, optional

Absolute pathname of file on web server. On Windows, use backward slashes; on UNIX, use forward slashes.

attributes
string, optional

One attribute (Windows) or a comma-delimited list of attributes (other platforms) to set on the file.

If omitted, the file's attributes are maintained.

destination
string, optional

Absolute pathname of directory or file on web server.

mode
string, optional

Applies only to Solaris and HP-UX. Permissions. Octal values of UNIX chmod command. Assigned to owner, group, and other, respectively.

filefield
string, optional

Name of form field used to select the file.

charset
string, optional

Character set name for the file contents.

nameconflict
string, optional

Action to take if filename is the same as that of a file in the directory.

  • skip
  • error
  • overwrite
  • makeunique
  • forceunique
accept
string, optional

list of supported extensions or/and mimetypes.

strict
boolean, optional

for action "upload" and "uploadAll":

if set to true (default), only mimetypes are supported.

createpath
boolean, optional

for action "append", "touch" and "write":

if set to false (default), expects all parent directories to exist, true will generate necessary directories

file
string, optional

Absolute pathname of file on web server.

variable
string, optional

Name of variable to contain contents of text file.

output
any, optional

Content of the file to be created.

cachedwithin
object, optional

possible values are:

  • String "request": If original content was created within the current request, cached content data is used.
  • a timespan (created with function CreateTimeSpan): If original content date falls within the time span, cached content data is used.

To use cached data, the tag must be called with the exact same arguments.

Only used by action "read" and "readbinary"

Introduced: 5.0.0.0

addnewline
boolean, optional

Yes: appends newline character to text written to file

fixnewline
boolean, optional
  • true: changes embedded line-ending characters in string variables to operating-system specific line endings
  • false: (default) do not change embedded line-ending characters in string variables.
result
string, optional

Name of the result value (default:cffile)

allowedextensions
any, optional

String list or string array of extensions allowed, this overwrites any other setting including the attribute [blockedExtensions] and settings done for example in the Application.cfc.

Alias: allowextension, allowedextension

Introduced: 5.3.8.107

blockedextensions
any, optional

String list or string array of extensions blocked, this overwrites any setting done for example in the Application.cfc.

Alias: blockextension, blockedextension

Introduced: 5.3.8.107

Examples

File Upload

<cffile action="upload" destination="destination-directory" fileField="form.fileData" nameconflict="overwrite">

File Write

<cffile action="write" file="#expandPath("./myFile.txt")#" output="Content that you need to write.">

File Read

<cffile action="read" file="#expandpath("./myfile.pdf")#" variable="myfile">

File Rename

<cffile action="rename" source="#expandPath("./myFile.pdf")#" destination="#expandPath("./myNewFileName.pdf")#" attributes="normal">

File Info

<cffile action="info" file="#expandPath("./myFile.txt")#" variable="fileInfo">

File Move

<cffile action="move" source="#expandpath("./myfile.pdf")#" destination="#expandpath("./some/moveditems/")#">

File Copy

<cffile action="copy" source="#expandpath("./myfile.pdf")#" destination="#expandpath("./some/copypath/")#">

File Delete

<cffile action="delete" file="#expandpath("./some/moveditems/")#">

File ReadBinary

<cffile action="readBinary" file="#expandPath("./myFile.txt")#" variable="fileData">

File Append

<cffile action="append" file="#expandPath("./myFile.txt")#" output="Content to append">

File Touch

<cffile action="touch" file="#expandPath("./myFile.txt")#" createpath=true>

Script Examples

File Info

result = fileInfo(getTempFile(getTempDirectory(),"demo"));

File Upload

result = fileupload(getTempDirectory(),"form.fileData"," ","makeunique");

File Write

filewrite(file="#expandPath("./myFile.txt")#" data="Content that you need to write.");

File Read

result = fileread(file="#expandPath("./myFile.txt")#");

File ReadBinary

binaryContent = fileReadBinary(expandPath('./image.jpg'));

File Append

fileAppend("path/to/file", "new content to append")

File Rename

filemove(source="#expandPath("./myFile.txt")#",destination="#expandPath("./myNewFileName.txt")#");

File Move

filemove(source="#expandPath("./myFile.txt")#",destination="#expandPath("./myNewFileName.txt")#");

File Copy

filecopy(source="#expandPath("./myNewFileName.txt")#",destination="#expandPath("./some/moved/")#");

File Delete

filedelete(source="#expandPath("./myFile.txt")#");

File Touch

fileTouch( file="#expandPath("./myFile.txt")#",createPath=true );

See also