Using the action Attribute to Perform Various Operations


You can use the action attribute of the <cffile> tag to specify actions other than uploading files on the server. You can move, copy, rename, read, and even write text files on the server by using the action attribute.

Manipulating Files on the Server

You can use the action attribute of the <cffile> tag to manipulate files that are located on the server. File manipulation involves

  • Copying a file

  • Renaming a file

  • Moving a file to a different directory

  • Deleting a file

Copying a File

To copy a file from a specified directory to a different location, you have to set the action attribute of the <cffile> tag to copy. To copy a file, you need to specify two additional attributes:

  • Source. This attribute is used to specify the absolute pathname of a file to be copied on the server computer, such as C:\myfiles\Resume.doc.

  • Destination. This attribute is used to specify the directory on the server where the file is to be copied, such as D:\backup\.

In the following code, the action attribute is used to copy a file in a directory on the server computer to a different directory:

 <html> <head> <title> Copying Files </title> </head> <body> <h2>Copying a File </h2> <cffile action="copy"    source="C:\myfiles\Readme.doc"    destination="D:\backup\" > </body> </html> 

Renaming a File

To rename a file in a specified directory, set the action attribute of the <cffile> tag to rename. In addition, two more attributes need to be specified:

  • Source. This attribute is used to specify the absolute pathname of a file to be renamed on the server computer, such as C:\myfiles\Resume.doc.

  • Destination. This attribute is used to specify the new file name that will overwrite the file name of the file to be renamed, such as C:\myfiles\MyResume.doc.

In the following code, the action attribute is used to rename a file in a directory on the server computer:

 <html> <head> <title> Renaming Files </title> </head> <body> <h2>Renaming a File </h2> <cffile action="rename"    source="C:\myfiles\resume.doc"    destination="C:\myfiles\Myresume.doc." > </body> </html> 

Moving a File

When you move a file from its current directory to a different directory, it's copied to the destination directory and then deleted from the current directory.

To move a file from a specified directory to a different location, set the action attribute of the <cffile> tag to move. To move a file, you need to specify two additional attributes:

  • Source. This attribute is used to specify the absolute pathname of a file to be moved to a different location on the server computer, such as C:\myfiles\myimage.gif.

  • Destination. This attribute is used to specify the directory on the server where the file is to be moved, such as D:\backup\.

The following code illustrates the use of the action attribute to move a file in a directory on the server computer to a different directory:

 <html> <head> <title> Moving Files </title> </head> <body> <h2>Moving a File </h2> <cffile action="move"    source="c:\myfiles\myimage.gif"    destination="c:\myfiles\" > </body> </html> 

Deleting a File

To delete a file in a specified directory, set the action attribute of the <cffile> tag to delete. To delete a file, a single additional attribute, file, needs to be specified. The file attribute is used to specify the absolute pathname of a file to be deleted on the server computer, such as C:\myfiles\Resumecopy.doc.

In the following code, the action attribute is used to delete a file in a directory on the server computer:

 <html> <head> <title> Deleting Files </title> </head> <body> <h2>Delete a File </h2> <cffile action="move" file="C:\myfiles\Resumecopy.doc" > </body> </html> 

Reading and Writing to a Text File

You can use the <cffile> tag to create, read, write, and modify text files on the server. This allows you to store information in text files, read that information using the <cffile> tag, and use it in your ColdFusion page.

Reading a Text File

To read a file in a specified directory, set the action attribute of the <cffile> tag to read. To read a file, you need to specify two additional attributes:

  • file. This attribute is used to specify the absolute pathname of a file to be read on the server computer (for example, C:\myfiles\Resume.txt).

  • variable. This attribute is used to specify the name of the ColdFusion variable that contains the contents of the text file.

The following code illustrates the use of the action attribute to read the text file C:\myfiles\Resume.txt in a specified directory on the server computer:

 <html> <head>    <title>Reading a Text File</title> </head> <body> Reading a text file. <cfset filecontents=""> <cffile action="read"    file="C:\myfiles\Resume.txt"    variable="filecontents"> The contents of the text file are as follows:<br> <cfoutput>   #filecontents# </cfoutput> </body> </html> 

Writing to a Text File

To create and write to a file in a specified directory, set the action attribute of the <cffile> tag to write. To write to a file, you need to specify two additional attributes:

  • file. This attribute is used to specify the absolute pathname of a file to be read on the server computer, such as C:\myfiles\newfile.txt.

  • output. This attribute is used to specify the name of the ColdFusion variable containing the text to be written to the file.

The following code illustrates the use of the write attribute to write to the text file D:\cfusionmx\wwwroot\newfile.txt on the server computer:

 <html> <head>    <title>Write to a Text File</title> </head> <body> <h2>Writing to a text file</h2> <cfset filecontents="This is a Sample Text File"> <cffile action="write"    file="D:\cfusionmx\wwwroot\newfile.txt"    output="The contents of the file are:#Variables.filecontents#" > </body> </html> </body> </html> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net