View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cffile action = "upload" PreviousNext

cffile action = "upload"

Copies a file to a directory on the server.

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

cfdirectory

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.

fileField

Required

 

Name of form field used to select the file.

Do not use pound signs (#) to specify the field name.

destination

Required

 

Pathname of directory in which to upload the file. If not an absolute path (starting a 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.

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 UNIX and Linux. 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

 

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.

  • readOnly
  • hidden
  • normal (if you use this option with other attributes, it is overridden by them)

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.

Tip: If your page is uploading a file that was selected on a form or was otherwise sent to your page via a multipart/form-data HTTP message, you can determine the approximate size of the file by checking the value of the CGI.content_length variable. This variable includes the file length plus the length of any other request content.

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

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.)

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>

Contents > CFML Reference > ColdFusion Tags > cffile action = "upload" PreviousNext

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

Version 6.1

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

Comments


uniquename2 said on Dec 15, 2003 at 6:05 AM :
This document doesn't clearly explain the scope of the "FileField" attribute. When CFFile can successfully upload a file using the value "form.picture_1" in the cf page processing the form request, it does NOT process "caller.form.picture_1" in any custom tags called by the same page. (Nor will any custom tags process that file if the field or the form is passed to them as an attribute.) This document needs an explanation of how this relates to the instructions not to use pound signs for this attribute.
Chris Hiester - MM Support said on Feb 5, 2004 at 12:12 PM :
It is important to note that if you don't specify a full path in the DESTINATION attribute of CFFILE ACTION="UPLOAD", the default location is relative to the GetTempDirectory() location. However, there must be a directory in that location that matches the string that you enter for the DESTINATION, otherwise you'll get a "string index out of range error." ColdFusion does not automatically create the directory if it does not already exist.
lbryngel said on Feb 18, 2004 at 8:41 AM :
We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.

FORM PAGE
<form method="post" action="tag_cffile_upload.cfm" name="uploadForm" enctype="multipart/form-data">
<input name="FileContents" type="file">
<br>
<input name="submit" type="submit" value="Upload File">
</form>

POST PAGE
<cffile action = "upload" fileField = "FileContents" destination = "c:\filesPload\" accept = "text/html" nameConflict = "MakeUnique">
lbryngel said on Feb 19, 2004 at 6:34 AM :
Ammendment to above example encompassing code into one page:

<!--- Windows Example --->
<!--- Check to see if the Form variable exists --->
<cfif isDefined("Form.FileContents") >
<!--- If TRUE then upload the file --->
<cffile action = "upload"
fileField = "FileContents"
destination = "c:\filesPload\"
accept = "text/html"
nameConflict = "MakeUnique">
<cfelse>
<!--- If FALSE then 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>
No screen name said on Apr 27, 2004 at 2:26 PM :
How can read a file from a dynamic location (such as a file on users computer)?
No screen name said on Jun 2, 2004 at 12:15 PM :
Add a form element to select the file:
<input type="File" name="image" size=40>
MaestroFJP said on Aug 23, 2004 at 4:16 AM :
Is the cffile "global" scope thread-safe in a persistant cfc enviroment or do you need to instantiate a new Input/Output object to maintain thread-safety?

You cannot "var" cffile to create a private variable at the begining of your method. After calling <cffile ...> is it sufficient to dupCffile = duplicate(cffile) to return at the end of the method?

Would it be thread-safe, if you temporarily instiantiate a new Input/Output cfc into the private scope (by var) in an persistant scope CFC in which your I/O would be gone when the method that called it was finished?
No screen name said on Dec 10, 2004 at 7:25 AM :
You can sometimes get the error "String index out of range:" when using CFFILE. What you may find if you google is that CFFILE needs the full path. That's ok, but sometimes the error can continue occurring.

The other thing that I found is that CFFILE doesn't like not having an extension on the filename. So saving an uploaded file as "abcdef12345" can sometimes throw this error.

To get around this, add in an extension, like ".tmp" because (I think) CFFILE looks for the position of the "." in the filename to create some of the CFFILE variables that can then be accessed by the script after the file upload has occurred.
tresise said on Dec 19, 2004 at 1:17 PM :
I was using cffile upload on a shared server using sandbox, got the error C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot-tmp\neotmp11917.tmp
Might be nice to cover what happens in these sorts of situations ie what errors actually mean.
bryanhur said on Jan 12, 2005 at 2:06 PM :
I agree. I'm fighting this right now and can't figure out why the upload won't happen.
wiredwizard said on Jan 17, 2005 at 1:15 PM :
I'm having same issue with cffile uploading to C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot- instead of destination but same file works fine on development server very confusing think it has something to do with jrun
rpress said on Feb 3, 2005 at 12:37 PM :
Is there a file size limit in transferring files this way? Can I move, for example, 4 Gb files? Is the entire file buffered in memory before writing to the file destination, or is it written to disk in chunks?

Thanks
wiredwizard said on Feb 15, 2005 at 1:02 PM :
no one ever covered why cffile upload sends file to C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot-tmp\neotmp11917.tmp

<cffile action = "upload"
fileField = "pic"
destination = "c:\inetpub\wwwroot\MYSITE\images\"
accept = "image/*" nameConflict = "MakeUnique">
wiredwizard said on Feb 17, 2005 at 8:31 AM :
Discovered the error of my ways

in reguard to cffile uploading to C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot-tmp\neotmp11917.tmp

1. be sure the destination directory already exist
2.use cffile insted of just file ie... <cfset pic1 = cffile.serverfile>
3.double check that your update statement is using correct name ie...
'#PIC1#' and not '#FORM.PIC1#'
abeBayquen said on Feb 20, 2005 at 6:44 PM :
I need to automate CFFILE somehow... is it possible to read the contents of a client directory and upload it's contents with CFFILE with the user indicating the directory or just one file in the directory?
No screen name said on Mar 9, 2005 at 7:35 PM :
I'd like to know the limits on file sizes, how this affects memory, etc.
Uploading a ~1.5GB file fails - "Document contains no data" - removing the <cffile> tag dosn't help - it appears to be a server limitation/setting - any info?
Jignesh Patel said on Mar 10, 2005 at 7:49 AM :
wiredwizard,
I dont understand your solution. Can you explain me how you solve your problem for <cffile action="upload">
jrunrandy said on Mar 14, 2005 at 1:40 PM :
There are many variables that determine the file-size limit. My understanding is that available JVM memory is the most important one.

You might try adjusting the -Xmx512m in the java.args line of cf_root/runtime/bin/jvm.config to something larger.

If that doesn't work, I suggest posting your question to the online forums: http:webforums.macromedia.com/coldfusion.
No screen name said on Apr 6, 2005 at 12:55 PM :
I want to Upload a file from a users PC, however, I don't want the user to specify the file to be uploaded (the filename and location are always the same). How can this be done?
Jignesh Patel said on Apr 6, 2005 at 1:04 PM :
You can upload a file using <CFFILE action = "Upload"> after successful upload, you need to rename the file with your preffered file name using <CFFILE action="rename">.
dea9dea9 said on Apr 7, 2005 at 6:43 AM :
To no screen name:

"I want to Upload a file from a users PC, however, I don't want the user to specify the file to be uploaded (the filename and location are always the same). How can this be done?"

I can understand why you want to do this for UI reasons, but that is a ridiculous security hazard. If I could do that I would loop through your internet explorer cookies files and steal all your passwords! HTML form fields require the user to choose the file and confirm that choice with the submit button. You can sometimes pre-populate the file name with javascript, but I never wanted to go that far.

-dea9
Tacs said on Jul 19, 2005 at 11:18 AM :
The documentation should also mention that it is impossible to find the clientFile name without uploading the file to a directory. This functionality is so that you can prepend an ID number to the beginning of a file before uploading it to a directory. This is a problem with the coding of coldfusion, and a new set of functionality would be required, an example of how PHP handles this problem can be found here: http://www.php.net/manual/en/features.file-upload.php

A better alternative is to use an array like PHP, in coldfusion syntax it would result in the following:
FORM.fileupload.fileSize
FORM.fileupload.clientFile
...
In this way, a user is not required to FULLY UPLOAD A FILE before they can even find the client's filename or the size of the file. This information MUST be available before hand, so that the user can even decide if they want to upload the file.
--------------------
A few HTML / file upload limitations that seem to be unclear:
-You can only upload one file at a time, furthermore you cannot upload a directory. (Opera 7 is an exception, but not well supported)
-You cannot set a default location to pull a file from. (This would pose a security threat, imagine the default of "/etc/passwd") One exception is that you can use Javascript to set a location, but if the location has been set through javascript an alert window appears when the submit button is pressed asking the user to confirm that file upload location
-All limitations in fileupload size are adjustable through coldfusion and webserver configuration files
-When the user submits a form with a fileupload, it is saved packet by packet into Coldfusion's temporary directory. Like the following, C:\CFusionMX\runtime\servers\default\SERVER-INF\temp\wwwroot-tmp\neotmp11917.tmp.
-When the <cffile action="upload...> command is executed, it simply moves the file from the temporary directory to the destination directory.
-Jignesh incorrectly stated that you need to rename the file after you upload it, you can rename it at the same time you move it by adding a filename to the destination.

All of these facts ought to be noted in the documentation.
No screen name said on Jul 24, 2007 at 2:31 PM :
accept = "image/jpg

in the docs is wrong. It should be accept="image/jpeg

jpg is not a proper mime type. See:

http://www.iana.org/assignments/media-types/image/

For a list.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p35.htm