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>

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

About This Tutorial
Author: David McIntosh
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX
Total Views: 68,257
Submission Date: May 05, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • 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

  • @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.

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

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

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

  • 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 code. Then set the DESTINATION from the 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!

  • 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?

  • This is a great tutorial, how can it be applied to upload Images, and I cant seem to get my upload to directory right.

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

  • #cfcatch.message#")> Upload


    #URL.NOTE#

    Select a File to Upload:

Advertisement

Sponsored By...
Powered By...