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.

Categories

File and Directory Access

Comments

comments powered by Disqus