Simple Coldfusion Upload Script

This tutorial will take you through the basics of creating a simple Coldfusion upload script. This script is ideal for uploading any file types. I will later be adding a tutorial on advanced file uploading with file type validation and size limiting.

To create an upload script the first thing we need is a form to select the file, so here is a quick example of a simple form with a file field and a submit button that submits to the current page.

<cfset CurrentPage=GetFileFromPath(GetTemplatePath())></cfset>
<form action="<cfoutput>#CurrentPage#</cfoutput>" method="post" enctype="multipart/form-data" name="Upload" id="Upload"></form>
<input type="file" name="File"/>
<input type="submit" name="Upload" value="Upload"/>
With that form created we will then need to create the script to handle the form and to upload the file, and this being Coldfusion they couldn't have made it easier for us.
<cfif IsDefined("FORM.Upload") AND #FORM.Upload# EQ "Upload">
<cffile action="upload" destination="D:\Domains\cfproject.co.uk\wwwroot\ColdfusionSamples\upload" filefield="FORM.File" nameconflict="overwrite">
File Uploaded!
</cfif>
Here we have a very simple script that just checks to see if the form has been submitted then using the cffile tag it uploads that file to the destination directory. In this tutorial i have set the conflict to overwrite, but you could also have error, skip or makeunique. This should be what you full file looks like.
<cfif IsDefined("FORM.Upload") AND #FORM.Upload# EQ "Upload">
<cffile action="upload" destination="D:\Domains\cfproject.co.uk\wwwroot\ColdfusionSamples\upload" filefield="FORM.File" nameconflict="overwrite">
File Uploaded!
</cfif>
<cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
<form action="<cfoutput>#CurrentPage#" method="post" enctype="multipart/form-data" name="Upload" id="Upload">
<input type="file" name="File"/>
<input type="submit" name="Upload" value="Upload"/>
</form>

Related Blog Entries

Comments
Fran12's Gravatar I tried it, but i did not work...offline...i dont know what is, if i test online, but my hoster don't allow to open .cfm scripts.
# Posted By Fran12 | 23/07/08 21:07
Jonny's Gravatar Does your server have ColdFusion installed?
# Posted By Jonny | 24/07/08 00:16
Jake's Gravatar I get a CFOutput error. It says: Context validation error for tag cfoutput.
The start tag must have a matching end tag. An explicit end tag can be provided by adding </cfoutput>. If the body of the tag is empty you can use the shortcut <cfoutput .../>.
# Posted By Jake | 12/08/09 21:56
Jonny Shaw's Gravatar well you obviously don't have matching <cfoutput> tags
# Posted By Jonny Shaw | 12/08/09 22:03
Jake's Gravatar ...and if you look at your code above, it doesn't have matching <cfoutput> tags either.
# Posted By Jake | 12/08/09 23:19
Alban's Gravatar Here is the error I get on the cffile line: String index out of range: -1
# Posted By Alban | 12/03/10 15:11

Archives By Subject

Advertising (4) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (16) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
Java (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Railo (4) [RSS]
SQL (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Simple Coldfusion Upload Script
Alban said: Here is the error I get on the cffile line: String index out of range: -1 [More]

Coldfusion 8 Functions
Mark said: Can Cold Fusion do math functions similar to say Excel? I need to make several calculations, and if ... [More]

BlogCFC on Railo
iphone clone said: Thanks for sharing [More]

Coldfusion Protx VSP Form Integration Kit
confinedspace said: @Dom: Cheers for that - helped me out. Just one thing to note, I would add "all" as a thir... [More]

Coldfusion to PHP?
Jonny Shaw said: PHP is very similar to most other OOP languages, which i am quite familiar with, so it wasn't really... [More]

Recent Entries

No recent entries.
ColdFusion Blog | ColdFusion Hosting | ColdFusion Q & A