In addition to managing files on the server, you can use cffile
to read, create, and modify text files. As a result, you can do the following things:
cflog
to create and write to log files.)
You can use cffile
to read an existing text file. The file is read into a local variable that you can 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.
<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>
You can use cffile
to write a text file based on dynamic content. For example, you could create static HTML files or log actions in a text file.
<html> <head> <title>Put Information into a Text File</title> </head> <body> <h2>Put Information into a Text File</h2> <form action="writetextfileaction.cfm" method="Post"> <p>Enter your name: <input type="text" name="Name" size="25"> <p>Enter the name of the file: <input type="text" name="FileName" size="25"> <p>Enter your message:</p> <textarea name="message"cols=45 rows=6></textarea> </p> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
writetextfileform.cfm
in myapps
under the Web root directory.
<html> <head> <title>Untitled</title> </head> <body> <cffile action="Write" file="C:\inetpub\wwwroot\mine\#Form.FileName#" output="Created By: #Form.Name# #Form.Message# "> </body> </html>
writetextfileaction.cfm
.writetextfileform.cfm
in your browser, enter values, and submit the form.The text file is written to the location you specified. If the file already exists, it is replaced.
You can use cffile action="Append"
to append additional text to the end of an existing text file, for example, when you create log files.