View comments | RSS feed

cffile action = "upload"

Description

Copies a file to a directory on the server.

Syntax

<cffile 
  action = "upload"
  fileField = "formfield"
  destination = "full_path_name"
  nameConflict = "behavior"
  accept = "mime_type/file_type"
  mode = "permission"
  attributes = "file_attribute_or_list">

See also

cfdirectory

History

New in ColdFusion MX: A directory path that you specify in the destination attribute does not require a trailing slash.

New in ColdFusion MX: on Windows platforms, the temporary, archive, and system options of the attributes attribute are deprecated. Do not use them in new applications. They might not work, and might cause an error, in later releases.

Attributes

Attribute Req/Opt Default Description
action
Required

Type of file manipulation that the tag performs.
fileField
Required

Name of form field used to select the file.
Do not use pound signs (#) to specify the field name.
destination
Required

Absolute pathname of directory or file on web server.
ColdFusion 5 and earlier: trailing slash in directory path is required. ColdFusion MX: trailing slash in directory path is optional. On Windows, use backward slashes; on UNIX, use forward slashes.
nameConflict
Optional
Error
Action to take if filename is the same as that of a file in the directory.
  • Error: file is not saved. ColdFusion stops processing the page and returns an error.
  • Skip: file is not saved. This option permits custom behavior based on file properties.
  • Overwrite: replaces file.
  • MakeUnique: forms a unique filename for the upload; name is stored in the file object variable serverFile.
accept
Optional

Limits the MIME types to accept. Comma-delimited list. For example, to permit JPG and Microsoft Word file uploads:
accept = "image/jpg, application/msword"
The browser uses file extension to determine file type.
mode
Optional

Applies only to Solaris and HP-UX. Permissions. Octal values of chmod command. Assigned to owner, group, and other, respectively. For example:
  • 644: Assigns read/write permission to owner; read permission to group and other
  • 777: Assigns read/write/execute permission to all
attributes
Optional

One attribute (Windows) or a comma-delimited list of attributes (other platforms) 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.
  • readOnly
  • hidden
  • normal (if you use this option with other attributes, it is overridden by them)

Usage

After a file upload is completed, you can get status information using file upload parameters. The status parameters use the cffile prefix; for example, cffile.clientDirectory. Status parameters can be used anywhere other ColdFusion parameters can be used.

Note:   The file prefix is deprecated, in favor of the cffile prefix. Do not use the file prefix in new applications.

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 Cold Fusion 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

Tip:   To refer to parameters, use the cffile prefix: for example, #cffile.fileExisted#.

Note:   File status parameters are read-only. They are set to the results of the most recent cffile operation. (If two cffile tags execute, the results of the second overwrite the first.)

Example

The following example creates a unique filename, if there is a name conflict when the file is uploaded on Windows:

<cffile action = "upload" 
  fileField = "FileContents" 
  destination = "c:\web\uploads\" 
  accept = "text/html" 
  nameConflict = "MakeUnique">

The following examples show the use of the mode attribute. The first example creates the file /tmp/foo with permissions defined as: owner=read/write, group=read, other=read.

<cffile action = "write"
file = "/tmp/foo" 
mode = 644
output = "some text"> 

This example appends to a file and sets permissions to read/write (rw) for all.

<cffile action = "append" 
destination = "/home/tomj/testing.txt" 
mode = 666 
output = "Is this a test?">

This example uploads a file and sets permissions to owner/group/other = read/write/execute.

<cffile action = "upload" 
fileField = "fieldname" 
destination = "/tmp/program.exe" 
mode = 777>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


h2oman said on Jun 19, 2002 at 9:13 PM :
Don't forget you can use cgi.content_length variable to check the size of the file before executing the cffile tag.

johnm@aiaa.org said on Feb 19, 2003 at 3:04 PM :
As of 19 February 2003 the CFFILE tag has a bug in what it returns as status parameters. The CFFILE.serverFileName status parameter returns the incorrect value if the attribute nameConflict = "MakeUnique" is set and the server actually makes a unique value. Instead of returning the new, unique, filename without the extension it returns the original file name (CFFILE.clientFileName). To get around this you can use this code (use the variable vFileServerName instead of CFFILE.serverFileName):
<cfparam name="vFileServerName" default="">
<cfparam name="vExt" default="">
<cfparam name="vExtSize" default="0">
<cfparam name="vNewFileName" default="">
<cfset vFileServerName = trim(cffile.serverFile)>
<cfset vExt = trim(right(vFileServerName, 4))>
<cfif find(".", vExt) GT 0>
<cfset vExtSize = len(mid(vExt, find(".", vExt), 4))>
<cfset vNewFileName = #left(vFileServerName, len(vFileServerName) - vExtSize)#>
<cfelse>
<cfset vNewFileName = #vFileServerName#>
</cfif>
hlichtin said on Mar 10, 2003 at 2:21 PM :
A bug has been entered against the makeunique problem.
onebaldman said on Apr 22, 2003 at 11:15 PM :
Remember ( I didn't see it documented throughout the docs) that you can't use # signs in the formfield attribute just pass it without them (cffile upload).

Cf throws the following error
No File found in field file

and has this as it's dump

C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot-tmp\neotmp4162.tmp example # with neotmp could be anything.

(in hopes that Google caches it for someone down the road getting the same bs that I did.)

Robby
walshd said on Apr 5, 2004 at 5:34 PM :
FYI

The folder that is used to store files during a cffile upload can collect large files. The file names start with neotmp and then some #.

C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot-tmp\ (default location)

I have seen this directory chew up disk space.
jrunrandy said on Apr 6, 2004 at 6:04 AM :
Thanks. Good tip. Also, in general you can delete the entire SERVER-INF/temp directory (when CF is down, of course) with no ill effects.
rcs_ said on Jun 29, 2004 at 11:28 AM :
In Cold Fusion MX 6.0, the value of the CFFILE tag parameter FILEFILE:

1) *must* be a field of scope FORM,
2) will always be overwritten with the value of the web server's SERVER-INF/temp directory, *even if copied to another variable* (even a session variable !)
3) the vales of cffile.clientfile, etc. will be undefined if the file is not of the appropriate MIME type or the tag does not execute successfully

As to the SERVER-INF/temp chewing up space, I'm not seeing this in MX 6.0 updater 3 so far.

We can't use MX 6.1 because our applications use the TIMEOUT parameter with CFQUERY, which doesn't work.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt129.htm