12 December 2008
Familiarity with Dreamweaver and ColdFusion.
Intermediate
File upload pages allow users to select files from their local hard drive and upload them to a server through a web browser. Building a file upload page with ColdFusion is quick and easy using the upload action of the CFFILE tag. CFFILE is a ColdFusion tag that can perform a variety of file management–related actions such as reading, writing, moving, renaming, and uploading files.
For the purpose of this article, I will focus on the upload action of this tag and provide Dreamweaver CS4 instructions for creating a simple ColdFusion file upload page. Although the step-by-step instructions provided in this article pertain to the Dreamweaver interface, the resulting code, which is listed at the bottom of this page, can be customized for use in other editors as well. The ColdFusion code used in the file upload example will work with versions 5 and above of ColdFusion Server.
Note: Adobe Dreamweaver Exchange lists the latest extensions available for download. In addition to the method provided in this article, you may also want to visit the Exchange to search for related extensions.
Allowing file uploads can potentially compromise server security. For this reason, it is a good idea to evaluate possible risks before implementing file uploads in a web application. There is no foolproof way to ensure a secure file upload in a web application, although through careful design, it is possible to reduce your risk.
Some suggestions for protecting server security:
Note: For additional information about server security, refer to Security Best Practice: Evaluating the Risks of Allowing Uploading and of Attached Files on Your Server (TechNote 17618).
The following steps provide instructions on how to create the HTML form. This simple HTML form will be the interface through which users select and upload files.
Note: For more information about defining a site, refer to Setting up a site in Dreamweaver CS4 / Setting up a site in Dreamweaver CS5 in Dreamweaver Help.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload File with ColdFusion</title>
</head>
<body>
<form action="cf_upload.cfm"
method="post" enctype="multipart/form-data" name="upload_form"></form>
</body>
</html>
Here is the completed HTML form code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload File with ColdFusion</title>
</head>
<body>
<form action="cf_upload.cfm" method="post" enctype="multipart/form-data" name="upload_form">
<input type="file" name="ul_path" id="ul_path" /><input name="upload_now" type="submit" value="Submit" />
</form>
</body>
</html>
Now that the HTML form is ready to go, you can add a few lines of CFML code using the Dreamweaver interface:
<body> tag and press Enter once to create a blank space. isdefined("form.upload_now")
Your CFIF tag should now match the code below:
<cfif isdefined("form.upload_now")></cfif>
Note: On Unix systems, make sure that the upload path directory is configured with the correct permissions so file upload can write to that directory. Normally, this directory would be owned by the ColdFusion user or the account that runs the web server.
Note: On Mac OS X systems, if you end up with a path with colons (:) in it—for example, Macintosh HD:Users:ray:Documents:Web Sites:webroot:cfuploaddwarticle—replace colons with forward slashes: /Users/ray/Documents/Web Sites/webroot/cfuploaddwarticle.
<cfif
isdefined("form.upload_now")>
<cffile
accept="image/gif,image/jpg,image/jpeg" action="upload" destination="/Users/ray/Documents/Web
Sites/webroot/cfuploaddwarticle" filefield="ul_path"
nameconflict="makeunique">
Thanks for uploading a file!
</cfif>
The complete code is listed below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload File with ColdFusion</title>
</head>
<body>
<cfif isdefined("form.upload_now")>
<cffile accept="image/gif,image/jpg,image/jpeg" action="upload" destination="/Users/ray/Documents/WebSites/webroot/cfuploaddwarticle" filefield="ul_path" nameconflict="makeunique">
Thanks for uploading a file!
</cfif>
<form action="cf_upload.cfm" method="post" enctype="multipart/form-data" name="upload_form">
<input type="file" name="ul_path" id="ul_path" />
<input name="upload_now" type="submit" value="Submit" />
</form>
</body>
</html>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Tutorials and samples |
| 04/23/2012 | Resolution/Compatibility/liquid layout |
|---|---|
| 04/20/2012 | using local/testing server with cs5 inserting images look fine in the split screen but do not show |
| 04/18/2012 | Ap Div help |
| 04/23/2012 | Updating |