ColdFusion lets you access and manage the files and directories on your ColdFusion server. The cffile tag has several attributes for moving, copying, deleting, and renaming files. You use the cfdirectory tag to list, create, delete, and rename directories. The cfcontent tag lets you define the MIME (Multipurpose Internet Mail Extensions) content type that returns to the web browser.
You can use the cffile tag to work with files on the server in several ways:
You use the action attribute to specify any of the following file actions: upload, move, rename, copy, delete, read, readBinary, write, and append. The required attributes depend on the action specified. For example, if action="write", ColdFusion expects the attributes associated with writing a text file.
File uploading requires that you create two files:
The following procedures describe how to create these files.
Create an HTML file to specify file upload information
The following table describes the code and its function:
Code |
Description |
---|---|
<form action="uploadfileaction.cfm" enctype="multipart/form-data" method="post"> |
Create a form that contains file selection fields for upload by the user. The action attribute value specifies the ColdFusion template that will process the submitted form. The enctype attribute value tells the server that the form submission contains an uploaded file. The method attribute is set to post to submit a ColdFusion form. |
<input type="file" name="FiletoUpload" size="45"> |
Allow the user to specify the file to upload. The file type instructs the browser to prepare to read and transmit a file from the user's system to your server. It automatically includes a Browse button to let the user look for the file instead of manually entering the entire path and filename. |
The user can enter a file path or browse the system and select a file to send.
The following table describes the code and its function:
Code |
Description |
---|---|
<cffile action="upload" |
Output the name and location of the uploaded file on the client machine. |
destination="c:\temp\" |
Specify the destination of the file. |
nameConflict="overwrite" |
If the file already exists, overwrite it. |
fileField="Form.FiletoUpload"> |
Specify the name of the file to upload. Do not enclose the variable in number signs. |
You uploaded #cffile.ClientFileName#.#cffile. ClientFileExt# successfully to #cffile.ServerDirectory#. |
Inform the user of the file that was uploaded and its destination. For information on scope variables, see Evaluating the results of a file upload. |