Multiple File Upload
Multiple File Upload
This is the quick and easy guide to getting cffile to upload MULTIPLE 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 'n' files. To upload files, the input tags need the type attribute set to file.
For this example, we will allow 5 files to be uploaded. This is going to be a hardcoded variable, but you could easily dynamicaly change this value depending on yout application or settings.
<cfset fileCount = 5>
Now let's buld the form dynamicaly based on this value. Each file field needs a unique name, so use file_n where n is the iteration count from the loop. This will allow us to loop through the file fields on the action page easily.
Also add a hidden field with the file count so we know how many to loop on the action page.
<form action="frmActUpload.cfm" method="post" enctype="multipart/form-data">
<cfoutput>
<cfloop from="1" to="#fileCount#" index="i">
File #i# <input type="file" name="file_#i#" /><br/>
</cfloop>
<cfoutput>
<input type="hidden" name="fileCount" value="#fileCount#" />
<input type="submit" name="fileUpload" value="Go!" />
</form>
That's it for the form, moving along to the action.
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, loop it, give it to cffile, and upload our files. 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 with error control, file information, and more.
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.
Start by looping from 1 to the value of the hidden field form.fileCount
<cfloop from="1" to="#form.fileCount#" index="i">
Now to get each filefield name and value dynamicaly. Treating the FORM scope as a structure helps make this simple, and avoids using evaluate.
Set a var for the field name used by cffile also.
<cfset thisFile = form["file_" & i]>
<cfset fieldName = "form.file_" & i>
Only try to proccess the file fild if it has a value!
<cfif len(thisFile) GT 0>
<cffile action="UPLOAD" destination="d:\inetpub\mySite\" filefield="#fieldName#">
</cfif>
</cfloop>
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.
____________
Comments
GiuliaN
01/26/2007
I tested this code in many ways, but I must say that it is not working.
Regards
Webmaster
01/27/2007
I sent an email, please reply! I would like to know what problem you had?
GiuliaN
01/29/2007
After testing again I found out that if I replace the notations:
form["file_" & i] and
"form.file_" & i with simply
"file_#i#" , the server accepts and process the code.
Thank you!
Webmaster
02/14/2007
Interesting that you had to use # inside the brackets. 'Best practices' would indicate NOT to do it that way - an overuse of the '#'
I've not run into a situation where you could not join a string to a variable where it's value is a string with & such as <cfset thisVar = "I Like ">
<cfset thatVar = thisVar & "ColdFusion">