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 (16) [RSS]
Image Manipulation (1) [RSS]
JavaScript (3) [RSS]
Useful Tools (3) [RSS]

Recent Comments

FCKeditor
Jonny Shaw said: I know that but unless you can get the new version on FCKeditor in to CF8 then i will keep using FCK... [More]

FCKeditor
Azadi Saryev said: Jonny, their ARE the same. CF8 just uses an earlier version of FCKEditor, so, of course, there are d... [More]

FCKeditor
Jonny Shaw said: There are a few differences between the two, i got annoyed with the CF8 editor because it tries to b... [More]

FCKeditor
Shannon Hicks said: Yeah, from the CF8 docs: Using the rich text editor The ColdFusion rich text editor lets users ente... [More]

FCKeditor
Henry Ho said: Ya.. YUI Editor also == FCKEditor [More]

Recent Entries

Customizable Homepage
Horizontal Loop
FCKeditor
Coldfusion Blog | Coldfusion Hosting | About Me