<cfset REQUEST.FileCount = 5 />
<cfset REQUEST.UploadPath = ExpandPath( "./uploads/" ) />
<cfloop
index="intFileIndex"
from="1"
to="#REQUEST.FileCount#"
step="1">
<cfparam
name="FORM.file#intFileIndex#"
type="string"
default=""
/>
</cfloop>
<cftry>
<cfparam
name="FORM.submitted"
type="numeric"
default="0"
/>
<cfcatch>
<cfset FORM.submitted = 0 />
</cfcatch>
</cftry>
<cfset arrErrors = ArrayNew( 1 ) />
<cfif FORM.submitted>
<cfset ArrayAppend(
arrErrors,
"Please select at least one file to upload"
) />
<cfloop
index="intFileIndex"
from="1"
to="#REQUEST.FileCount#"
step="1">
<cfif Len( FORM[ "file#intFileIndex#" ] )>
<cfset ArrayClear( arrErrors ) />
<cfbreak />
</cfif>
</cfloop>
<cfif NOT ArrayLen( arrErrors )>
<cfset arrUploaded = ArrayNew( 1 ) />
<cfloop
index="intFileIndex"
from="1"
to="#REQUEST.FileCount#"
step="1">
<cfif Len( FORM[ "file#intFileIndex#" ] )>
<cftry>
<cffile
action="upload"
destination="#REQUEST.UploadPath#"
filefield="file#intFileIndex#"
nameconflict="makeunique"
/>
<cfset ArrayAppend(
arrUploaded,
(CFFILE.ServerDirectory & "\" & CFFILE.ServerFile)
) />
<cfcatch>
<cfset ArrayAppend(
arrErrors,
"There was a problem uploading file ###intFileIndex#: #CFCATCH.Message#"
) />
<cfbreak />
</cfcatch>
</cftry>
</cfif>
</cfloop>
<cfif ArrayLen( arrErrors )>
<cfloop
index="intFileIndex"
from="1"
to="#ArrayLen( arrUploaded )#"
step="1">
<cftry>
<cffile
action="delete"
file="#arrUploaded[ intFileIndex ]#"
/>
<cfcatch>
</cfcatch>
</cftry>
</cfloop>
<cfelse>
</cfif>
</cfif>
</cfif>
<cfcontent
type="text/html"
reset="true"
/>
<!DOCTYPE html PUBLIC "- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Multiple File Uploads</title>
</head>
<body>
<cfoutput>
<h1>
Multiple File Upload ColdFusion Example
</h1>
<cfif ArrayLen( arrErrors )>
<p>
Please review the following errors:
</p>
<ul>
<cfloop
index="intError"
from="1"
to="#ArrayLen( arrErrors )#"
step="1">
<li>
#arrErrors[ intError ]#
</li>
</cfloop>
</ul>
</cfif>
<form
action="#CGI.script_name#"
method="post"
enctype="multipart/form-data">
<input type="hidden" name="submitted" value="1" />
<cfloop
index="intFileIndex"
from="1"
to="#REQUEST.FileCount#"
step="1">
<label for="file#intFileIndex#">
File #intFileIndex#:
</label>
<input
type="file"
name="file#intFileIndex#"
id="file#intFileIndex#"
/>
<br />
</cfloop>
<input type="submit" value="Upload Files" />
</form>
</cfoutput>
</body>
</html>