Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How can I limit the size of a file when uploading with cffile?

You can use the cgi.content_length to find the file size. If the cgi.content_length is less than or equal to the maximum size specified the file is uploaded (saved) to the server.

<cfset fileSizeLimit = 60000 />
<cfif cgi.content_length LTE fileSizeLimit>
   <cffile action="UPLOAD"
filefield="Filename"
destination="#thisDirectory#"
nameconflict="OVERWRITE"
accept="image/gif, image/jpeg">

   <center>SUCCESS!</center>
<cfelse>
   <cfoutput>
   <center>
   Your file size of #cgi.content_length# is too big!
   <br>
   The maximum size allowed is #fileSizeLimit#.
   </center>
   </cfoutput>
   <cfabort>
</cfif>

Please note with this example the file is still uploaded as a temporary file, but the file is not saved. File sizes can also be restricted in the ColdFusion Administrator by using the setting, "Maximum size of post data." However, the example above gives more granular control.


This question was written by Stan Winchester.
It was last updated on January 30, 2006 at 7:28:06 AM EST.

CFML Referenced

<cfif>
<cfoutput>
<cfelse>
<cfabort>

Categories

File and Directory Access

Comments

There are no comments for this entry.