Copies a file to a directory on the server.
<cffile action = "upload" destination = "full pathname
" fileField = "form field
" accept = "MIME type
|file type
" attributes = "file attribute or list
" mode = "permission
" nameConflict = "behavior
" result = "result name
">
See the History section of the main cffile tag page.
Attribute |
Req/Opt |
Default |
Description |
---|---|---|---|
action |
Required |
|
Type of file manipulation that the tag performs. |
destination |
Required |
|
Pathname of directory in which to upload the file. If not an absolute path (starting with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function. |
fileField |
Required |
|
Name of form field used to select the file. Do not use number signs (#) to specify the field name. |
accept |
Optional |
|
Limits the MIME types to accept. Comma-delimited list. For example, the following code permits JPEG and Microsoft Word file uploads: accept = "image/jpg, application/msword" The browser uses the file extension to determine file type. |
attributes |
Optional |
|
Applies to Windows. A comma-delimited list of attributes to set on the file. If omitted, the file's attributes are maintained. Each value must be specified explicitly. For example, if you specify attributes = "readOnly", all other attributes are overwritten.
|
mode |
Optional |
|
Applies only to UNIX and Linux. Permissions. Octal values of chmod command. Assigned to owner, group, and other, respectively, for example:
|
nameConflict |
Optional |
Error |
Action to take if filename is the same as that of a file in the directory.
|
result |
Optional |
Lets you specify a name for the variable in which cffile returns the result (or status) parameters. If you do not specify a value for this attribute, cffile uses the prefix cffile. For more information, see Usage. |
After a file upload is completed, you can get status information using file upload parameters. To refer to parameters, use either the cffile prefix or, if you specified an alternate name in the result attribute, the name you specified there. For example, if you did not specify a name in the result attribute, access the fileExisted parameter as #cffile.fileExisted#. If you set the result attribute to myResult, however, access fileExisted as #myResult.fileExisted#.
Status parameters can be used anywhere that other ColdFusion parameters can be used.
When you use a cfform tag or an HTML form tag to submit the form with the file to be uploaded, you must specify enctype="multipart/form-data" in the tag, as shown in the example for this tag. By default, ColdFusion MX 7 sends the form with the encoding type of application/x-www-form-urlencoded, which causes an error in the cffile tag.
The following file upload status parameters are available after an upload:
Parameter |
Description |
---|---|
attemptedServerFile |
Initial name ColdFusion used when attempting to save a file |
clientDirectory |
Directory location of the file uploaded from the client's system |
clientFile |
Name of the file uploaded from the client's system |
clientFileExt |
Extension of the uploaded file on the client system (without a period) |
clientFileName |
Name of the uploaded file on the client system (without an extension) |
contentSubType |
MIME content subtype of the saved file |
contentType |
MIME content type of the saved file |
dateLastAccessed |
Date and time the uploaded file was last accessed |
fileExisted |
Whether the file already existed with the same path (yes or no) |
fileSize |
Size of the uploaded file |
fileWasAppended |
Whether ColdFusion appended uploaded file to a file (yes or no) |
fileWasOverwritten |
Whether ColdFusion overwrote a file (yes or no) |
fileWasRenamed |
Whether uploaded file renamed to avoid a name conflict (yes or no) |
fileWasSaved |
Whether ColdFusion saves a file (yes or no) |
oldFileSize |
Size of a file that was overwritten in the file upload operation |
serverDirectory |
Directory of the file saved on the server |
serverFile |
Filename of the file saved on the server |
serverFileExt |
Extension of the uploaded file on the server (without a period) |
serverFileName |
Name of the uploaded file on the server (without an extension) |
timeCreated |
Time the uploaded file was created |
timeLastModified |
Date and time of the last modification to the uploaded file |
The following example creates a unique filename, if there is a name conflict when the file is uploaded on Windows:
<!--- Windows Example ---> <!--- Check to see if the Form variable exists. ---> <cfif isDefined("Form.FileContents") > <!--- If TRUE, upload the file. ---> <cffile action = "upload" fileField = "FileContents" destination = "c:\files\upload\" accept = "text/html" nameConflict = "MakeUnique"> <cfelse> <!--- If FALSE, show the Form. ---> <form method="post" action=<cfoutput>#cgi.script_name#</cfoutput> name="uploadForm" enctype="multipart/form-data"> <input name="FileContents" type="file"> <br> <input name="submit" type="submit" value="Upload File"> </form> </cfif>