Simple File Upload -Single File
Single File Upload
This is the quick and easy guide to getting cffile to upload files.
The Form
We'll name this script frmUpload.cfm
The for that the user will choose a file to upload from MUST have the following two attributes: method="post" and enctype="multipart/form-data". This is REQUIRED for uploading a file with cffile.
Let's build a simple form to upload a file. To upload a file, an input tag with the type attribute set to file is also required.
<form action="frmActUpload.cfm" method="post" enctype="multipart/form-data">
<input type="file" name="uploadThis" /><br/>
<input type="submit" name="fileUpload" value="Go!" />
</form>
That's it for the form, told you I was keeping it simple.
The Action Page
Let's call this page frmActUpload.cfm since that what I specified for the form action.
Here we are going to take the form info, give it to cffile, and upload our file. I am leaving out ALL checking and control for this to make it simple. You seriously need to consider the security risks involved in allowing users to upload files. Check for a more advanced tutorial on cffile.
Cffile action="upload" needs three attributes: action, destination, and filefield. There are several other optional attributes, please take the time to look into them!
Filefield is the form field that we used to choose the file for upload. In this case, we called it 'uploadThis'. So FORM.uploadThis is the filefield.
Destination is a physical path on the hard drive of the SERVER where the file should be stored. The ColdFusion server MUST have access to this directory, but the directory does not need to by accessible by the web server.
<cffile action="UPLOAD" destination="d:\inetpub\mySite\" filefield="form.uploadThis">
Problems & Troubleshooting
A few things to watch out for with cffile upload.
I have had cffile upload fail on directories that contained periods and/or spaces. This may be fixed in most versions now.
The most common issue for cffile upload to not work is that you are trying to save the file to a directory that the ColdFusion server does not have access to.
Tied for the most common issue may also be forgetting to put enctype="multipart/form-data" in the html form tag.
____________