This is simple… simple I say!!! Uploading files to your website using Coldfusion is actually a Simple process… There is nothing really secret or sinister about this so.. here we go:
First we need a form. See… Simple!!! Everyone can do a form. Lets say we are letting people upload an image to the site:
<form name=”uploadImage” action=”uploadimage.cfm” method=”post” enctype=”multipart/form-data”>
<h2>Upload your image</h2>
<td>Upload your image</td>
<td><input name=”image” type=”file”></td>
<td><input type=”reset” value=”Clear Entry”></td>
<td><input type=”submit” value=”Upload the Image”></td>
Now we’ve got our form! Hurrahh!! See, Easy! Now for this form to work the way we want, we need to make sure and make the “enctype” set to “multipart/form-data” Basically this tells the form that it is submitting more than just text.
Check out the <a href=”http://www.w3.org/TR/html4/interact/forms.html#h-22.214.171.124″>W3C </a> for more information on html form use.
Ok. Now that we have our form we need a way to process the information. notice, in the form I set the “action” to “uploadimage.cfm”. That is telling our form.. Ok take this information and send it to this file for processing.
No our upload form could look something like this:
<cffile action=”upload” destination=”x:\FakeDirectory\www\images\” nameconflict=”overwrite” filefield=”image”>
Now… is that it??? Sure thing. This takes the contents of the “filefield” from our upload form and saves it to our Fake directory located on the server. It will also look at the file name, if it already exists it will just overwrite the file with the new version.
Whew!!! See… Simple… Now, this is just a BASIC example of how to upload files…. This will not check what type of file is uploaded or how big it is.. Those are two things that you would want to do before keeping the file on your server.
But this is a start… I’ll give an example soon on how to check for file type and set a max size.
© 2007 – 2009, Robert Owen. All rights reserved.