Server Time:
Sunday May 11 2008 08:18 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

File Upload Size Checker
by: David McIntosh
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

When you allow file uploading... Set a File Max Size and reject the file if it is too large.

Others will check it after it is uploaded.. then deletes it if too large.. but not this... it won't let them even upload a single bit... if it is too large! 

It will upload the file to a folder in the C directory called “uploads” so you need to have the tag available and access to the path.

////////////////////////////////////////////////////////


<!--- upload/index.cfm --->

<CFIF isdefined ("FORM.Upload")>

    <CFIF VAL(CGI.CONTENT_LENGTH) GT 20000>

         <cflocation addtoken=
"no"
                         url=
"index.cfm?Note=File too Large! (#VAL(CGI.CONTENT_LENGTH/1000)# KB)">
         <CFABORT>

    <CFELSE>

       <CFFILE 
                ACTION=
"Upload" 
                FILEFIELD=
"UploadMe" 
                DESTINATION=
"C:\uploads\" 
                NAMECONFLICT=
"OVERWRITE">

       <cflocation addtoken=
"no"
                       url=
"index.cfm?Note=File Uploaded (#VAL(CGI.CONTENT_LENGTH/1000)# KB)">
       <CFABORT>

    </CFIF>

</CFIF>


<html>
  <head>
     <title>
Upload</title>
  </head>

<body>


<cfparam name="URL.Note" default="">
<CFOUTPUT>
   <strong><font color=
"RED">#URL.NOTE#</font></strong>
</CFOUTPUT>

<form action="index.cfm" method="post" enctype="multipart/form-data">
<input name="UPLOAD" type="hidden" value="Y">
Source File Name:<BR>
<INPUT NAME="UploadMe" TYPE="File" SIZE="50"><BR>
<input type="Submit" value="Upload Now">
</form>


</BODY>
</HTML>

////////////////////////////////////////////////////////


Date added: Mon. May 5, 2003
Posted by: David McIntosh | Views: 17487 | Tested Platforms: CFMX | Difficulty: Intermediate
Categories Listed: Uploading

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
An CFABORT after a CFLOCATION?
Great tutorial!

There is one question though..
Why the CFABORT after the CFLOCATION?
CFLOCATION stops the execution of the current page and opens the the new page. So CFLOCATION has already got the CFABORT function in itself.. or am I wrong?
Posted by: Frederik
Posted on: 09/14/2004 04:14 AM
I Updated my Code

<cfparam name="FORM.UploadMe" default="">
<cfparam name="FORM.CMD" default="">
<cfparam name="URL.Note" default="">


<CFIF FORM.CMD eq "upload">
<CFIF VAL(CGI.CONTENT_LENGTH) GT 100000>
<CFSET SendNote = URLEncodedFormat("File too Large! (#VAL(CGI.CONTENT_LENGTH/1000)# KB)")>
<cflocation addtoken="no" url="#CGI.SCRIPT_NAME#?Note=#SendNote#">
<CFELSE>

<cftry>
<CFFILE
ACTION="Upload"
FILEFIELD="UploadMe"
DESTINATION="d:\websites\secure.uncc.edu\www\test\david"
NAMECONFLICT="OVERWRITE"
accept="application/pdf">
<cfcatch>
<CFSET SendNote = URLEncodedFormat("Error with upload.<br>#cfcatch.message#")>
<cflocation addtoken="no" url="#CGI.SCRIPT_NAME#?Note=#SendNote#">
</cfcatch>
</cftry>

<CFSET SendNote = URLEncodedFormat("File Uploaded (#VAL(CGI.CONTENT_LENGTH/1000)# KB)")>
<cflocation addtoken="no" url="#CGI.SCRIPT_NAME#?Note=#SendNote#">
</CFIF>
</CFIF>

<html>
<head>
<title>Upload</title>
</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<CFIF TRIM(URL.Note) neq "">
<tr>

<td height="25" align="center">
<hr width="350" size="1" noshade>
<font color="#CC3333" size="2" face="Arial, Helvetica, sans-serif"><strong><CFOUTPUT>#URL.NOTE#</CFOUTPUT> </strong></font>
<hr width="350" size="1" noshade><br>
</td>
</tr>
</CFIF>

<tr>
<td><form action="<cfoutput>#CGI.SCRIPT_NAME#</cfoutput>" method="post" enctype="multipart/form-data">


<table width="350" border="0" align="center" cellpadding="10" cellspacing="2" bgcolor="#333399">
<td align="left" nowrap bgcolor="whitesmoke"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Select
a File to Upload:</strong></font><br>
<input name="UploadMe" type="File" class="smallnormal" size="25">
<input name="Submit" type="Submit" class="smallnormal" value="Upload File >">
</td>
</tr>
</table>

<input type="hidden" name="CMD" id="CMD" value="upload">


</form></td>
</tr>
<tr>
<td> </td>
</tr>
</table>

</BODY>
</HTML>

Posted by: David McIntosh
Posted on: 10/20/2004 02:17 PM
Behind the Code
The changes now correctly handle the URL.Note URL Encoding. NON-IE users will like it better than an error.

ALso I added Error delivery in the note for people that want to restrict the file type. In this case if they try to upload something other than a pdflike a .txt file, they get an error that says:
Error with upload.
The MIME type of the uploaded file "text/plain" was not accepted by the server.

In fact if they submit nothing they get:
Error with upload.
The form field "UploadMe" did not contain a file.

Hope this helps the many people that asked me questions.

Posted by: David McIntosh
Posted on: 10/20/2004 02:21 PM
Uploading Pics
This is a great tutorial, how can it be applied to upload Images, and I cant seem to get my upload to directory right.
Posted by: Dean
Posted on: 01/13/2005 03:41 PM
why don't work for me?
The first version is woking fine but the updated give this error: Error with upload.Error processing CFFILE tag - otherway if the file is big I get the correct message: File too Large!

What could be the problem?
Posted by: spuri
Posted on: 09/05/2005 10:19 AM
Upload Destination
I love this tutorial. It's very easy to understand and implement. However, I ran into a minor roadblock when trying to set the DESTINATION for the uploaded image. Since I'd be uploading an image from a computer to the server, a destination such as D:\Pics\New\etc. doesn't work. So I found the following code in a forum and placed it just above the <cftry> code.

<!--- Set "uploadDir" to whatever directory you are uploading to. i.e. C:\yourwebsite\uploads--->
<cfset uploadDir = getDirectoryFromPath(expandPath("*")) & "Pics/New/">

Then set the DESTINATION from the <cffile> equal to "#uploadDir#" rather than what it's at now (ie. rather than D:\Pics\New\etc.).

Notice I can now use forward slashes for web-use instead of having to use the backward slashes from before.

Hope this helps!
Posted by: Douglas
Posted on: 11/06/2005 04:04 PM
A slight lie
The following line is a bit flawed ..

"Others will check it after it is uploaded.. then deletes it if too large.. but not this... it won't let them even upload a single bit... if it is too large!"

Because if you implement this upload function and then validate the file size using the #CGI.CONTENT_LENGTH# variable. It still uploads the file into onto the server's tempary directory before obtaining the result.

So while it doesn't save the file to the server and then delete it, it does require the browser to still upload the file to the server .. so it is not saving the uploader any time or bandwidth.
Posted by: Ipggi
Posted on: 12/06/2005 12:13 AM
Very disappointed
EasyCFM is a great site, however, someone ought to reviews the tutorials before they are posted. Why? Well, it's called EasyCFM.com for a reason, it's supposed to make it easy for newbies/beginners and intermediates to learn how to be better programmers. Then you have people like this SmackAndToss (oops! McIntosh) posting tutorials full of errors. I mean, c'mon, if you can come up with something like this tutorial, it seems you at least ought to be able to make sure it works, and works properly. Like I said, this site is mostly for beginners and having people post tutorials like this isn't going to do anything but turn newbies off to programming.

I know what most of you are probably thinking...well, if you're so smart and can do better, where's your great tutorials? I here to learn, not to teach. If I was here to teach, I'd post my own tutorials. By posting tutorials, you're teaching others how to do things. So, if I go to college to learn and the professor/teaching assistant gives a lecture and the information is full of errors, I'm thinking maybe I'm wasting my time and need to find a better place.
Posted by: Miles
Posted on: 05/04/2006 06:39 PM
Need a basic uploading files
hai all...

i need help to upload a files to the server...
the basic uploading...not the checking of files or something else...

appreciate and advanced thank you for response me...
Posted by: afni
Posted on: 11/20/2006 11:16 PM
Reply to Miles
@Miles
I wrote this "tutorial" over two years ago. If you were not such an ass I would have been glad to look at this rather OLD code and see what problems you were having. But... you pretty much burnt me from ever wanting to contribute to this forum ever again. I thought I was helping... given the countless emails I have gotten thanking me... but then you come along... a total leach wanting things handed to you. Good luck trying to learn with that attitude.

TO everyone else... I have moved on to PHP, JSP, and other languages. I still develop the occasional app in CFML but only when needed. If I can help you (other than the above shit) please let me know directly.

Have a nice day.

Posted by: David McIntosh
Posted on: 11/21/2006 12:23 AM
Reply to Miles/David
Miles,

Tutorial authors do a great job submitting tutorials for individuals to learn from. I understand the concern for errors and misspellings; however authors are human. I feel that if something has an error or needs to be updated that is understandable; however approaching the situation as aggressive as you have makes me wonder why. I mean; David worked on something and he felt it was good and needed. He took a leap of faith to help others and that demands respect and appreciation. I know that this tutorial has helped many people... sure they might have to think for themselves a bit and fix a bug; but ultimately that is what programming is. If someone gets turned off of programming by a bug; then they were not meant to be programmers.

David,
I thank you for you submission (as do countless others that have used it and learned from it). I hope you enjoy PHP, JSP and other languages; but always remember your roots ;)

Thanks again,
Pablo Varando
webmaster@easycfm.com

Posted by: Pablo Varando
Posted on: 11/21/2006 02:32 PM
Need help
Hi,

Im trying to have my user upload files to my server. After reading the sample above, Im not quite sure what is the "1000" in "(#VAL(CGI.CONTENT_LENGTH/1000)# KB"?

If the file is too large, will it store in my directory? Can I automatically delete the file if the file is larger from what I requested?

Thanks for the wonderful tutorial.


Posted by: Pino
Posted on: 01/01/2007 06:29 PM
A way to check to make sure it's a file?
Is there a way to check if the form field is actually a file?
Posted by: Nate
Posted on: 01/31/2007 05:05 PM
Update
Sorry...Should really be 1024
1,024 kilobytes (KB) are equal to 1 megabyte (MB or Meg)


You would need to set an upper KB limit:
<CFIF VAL(CGI.CONTENT_LENGTH) GT 100000> This = ~97.65MB

When you run the post from the form... using the enctype="multipart/form-data" it is actually stored on the server in a temp area... the CGI.CONTENT_LENGTH check then allows the actual "upload" to your specified directory path.

I believe the server itself already manages these temp files... so there is nothing to clean up; But I would love to be corrected if that is not the case.

- neofactor
Posted by: neofactor
Posted on: 01/31/2007 07:24 PM
Special Characters in File Names?
Great tutorial. How would you handle files uploaded with special characters in the name such as spaces(from mac users) and extended characters (like from a portuguese speaking country)?

Posted by: PsyberX
Posted on: 06/27/2007 01:50 AM
Getting Just the Filename
Hi Great Script!

I have a heravil modified version of this script writting the filename to a database (for a web application) and Yes the File Size and the Filename are getting inserted correctly into my database, except when I go check my records the filename comes up as:
C:\CFusionMX7\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp26132.tmp

Rather than the podcast.mp3 file I origianlly uploaded.

Anyone got any ideas?

Posted by: JHarvey
Posted on: 07/10/2007 02:16 PM
multiple uploads
How does the CGI.CONTENT_LENGTH works if my form has the ability to upload multiple files?

Is this correct?

<cfif cgi.CONTENT_LENGTH lt 25000>
<cffile action="upload"
fileField="testFile1"
destination="/tmp"
nameConflict="overwrite" />
</cfif>
<cfif cgi.CONTENT_LENGTH lt 25000>
<cffile action="upload"
fileField="testFile2"
destination="/tmp"
nameConflict="overwrite" />
</cfif>
<cfif cgi.CONTENT_LENGTH lt 25000>
<cffile action="upload"
fileField="testFile3"
destination="/tmp"
nameConflict="overwrite" />
</cfif>
.
.
.
Posted by: Jimmy
Posted on: 08/08/2007 05:10 PM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
CF 8 Hosting for less then $40 a year!

You are 1 of 787 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb