Writing and Appending to Files

Contact Us or call 1-877-932-8228
Writing and Appending to Files

Writing and Appending to Files

When writing or appending to a file, <cffile> requires three attributes:

<cffile> Attributes for Writing Files
Attribute Description
action Must be set to write or append.
file The absolute path to the file to write.
output The text to write to the file.

The addnewline attribute is also useful. It takes a Yes/No value and determines whether a hard return should be appended to the output.

Note: When ColdFusion tries to append to a file that doesn't exist, it will create the file if it has permission to do so and the specified directory exists.

Uploading a New File

When uploading a new file, <cffile> requires three attributes:

<cffile> Attributes for Uploading Files
Attribute Description
action Must be set to upload.
fileField The form variable that contains the uploaded file.
destination The absolute path to the directory in which to save the file.

Two other useful, but not required, attributes are nameconflict and accept.

Additional <cffile> Attributes for Uploading Files
Attribute Description
nameconflict Possible values are error, overwrite, skip, and makeunique. error is the default.
accept A list of file types that the can be uploaded.

The following demo allows a user to upload a new log file to the Logs directory. The screenshot below shows the form.

And here is the results page.

Code Sample:

FilesAndDirs/Demos/AddLog.cfm
<html>
<head>
  <title>Add New Log</title>
</head>
<body>

<cfif isDefined("FORM.submitted")>
	<cfset uploaddir = ExpandPath("Logs")>
	<cffile action="upload" filefield="FORM.logfile"
				destination="#uploaddir#" accept="text/plain"
				nameconflict="#FORM.conflict#">
	<h2>Log Added</h2>
	<a href="RunningLogList.cfm">See All Running Logs</a>

<cfelse>
<h1>Add New Log</h1>
<cfoutput>
<form method="post" enctype="multipart/form-data" action="#CGI.SCRIPT_NAME#">
</cfoutput>
<input type="hidden" name="submitted" value="true">
<p>Log: <input type="file" name="logfile" accept="text/plain"></p>
<p>What do you want to do if a file by this name already exists?</p>
<select name="conflict">
	<option value="makeunique">Create New Name</option>
	<option value="overwrite">Overwrite Existing File</option>
	<option value="error">Don't Overwrite and Report Error</option>
	<option value="skip">Don't Overwrite and Ignore</option>
</select>
<p align="center"><input type="submit" value="Add Log"></p>
</form>

</cfif>

</body>
</html>

Things to note about this code:

  • This page has a self-submitting form.
  • The <form> tag takes the attribute enctype with the value set to multipart/form-data. This allows file uploads.
  • The form has a file input field, which allows the user to browse to a file on the client machine. The <cffile> tag uses this form variable as its filefield.
  • The form has a select menu, which allows the user to determine how a file name conflict is handled. The <cffile> tag uses this form variable as the value of its nameconflict attribute.
Next