Reading, writing, and appending to a text file
In addition to managing files on the server, you can use
files. As a result, you can do the following things:
•
Create log files. (You can also use
•
Generate static HTML documents.
•
Use text files to store information that can be incorporated into web pages.
Reading a text file
You can use
cffile
use anywhere in the application page. For example, you could read a text file and then insert its
contents into a database, or you could read a text file and then use one of the string replacement
functions to modify the contents.
To read a text file:
Create a ColdFusion page with the following content:
1
<html>
<head>
<title>Read a Text File</title>
</head>
<body>
Ready to read the file:<br>
<cffile action="read"
file="C:\inetpub\wwwroot\mine\message.txt"
variable="Message">
<cfoutput>
#Message#
</cfoutput>
</body>
</html>
Replace C:\inetpub\wwwroot\mine\message.txt with the location and name of a text file on the
2
server.
Save the file as readtext.cfm in the myapps directory under your web_root and view it in the
3
browser.
Writing a text file on the server
You can use
cffile
static HTML files or log actions in a text file.
To create a form in to capture data for a text file:
Create a ColdFusion page with the following content:
1
<html>
<head>
<title>Put Information into a Text File</title>
</head>
<body>
<h2>Put Information into a Text File</h2>
826
Chapter 37: Managing Files on the Server
cflog
to read an existing text file. The file is read into a local variable that you can
to write a text file based on dynamic content. For example, you could create
to read, create, and modify text
cffile
to create and write to log files.)