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#" 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

Archives By Subject

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

Recent Comments

Coldfusion and Java
Henry Ho said: I'm more interested in writing Groovy with CFGroovy. I think you may want to look into that as well... [More]

Coldfusion and Java
Jeremy P said: Since CF is build on Java, it's very easy/handy to use it. I recently had to "drop down"... [More]

Coldfusion and Java
Kumar said: Similarly to what Ben said, we have used Java objects in ColdFusion to perform certain tasks. Dan V... [More]

Coldfusion and Java
Ben Nadel said: I use Java objects in ColdFusion all the time. The CreateObject( "java" ) method makes it ... [More]

Coldfusion and Java
Steve Brownlee said: Yes, I can provide some advantages, but that's a large discussion. It can provide speed advantages ... [More]

Recent Entries

Coldfusion and Java
Coldfusion Hosting From £2.50
Coldfusion Blog | Coldfusion Hosting | About Me