Avg. Rating 3.0

Problem

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

Solution

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.

Detailed explanation

<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 recipe was originally contributed by Stan Winchester.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes