<cfprocessingdirective pageEncoding="utf-8">
<!---This variable specifies relative path to the folder, where the gallery with uploaded files is located.
Do not forget about the slash in the end of the folder name.--->
<cfset galleryPath="../Gallery/" />
		
<cfset absGalleryPath="#ExpandPath(galleryPath)#" />
<cfset absThumbnailsPath="#absGalleryPath#Thumbnails/" />
			
<!---First of all, clear files and data uploaded at previous time.--->

<!---Delete source files.--->
<cfdirectory name="galleryDir" action="list" 
			directory="#absGalleryPath#" filter="*" />
<cfloop query="galleryDir">
	<cfif FileExists("#absGalleryPath##Name#")>
		<cffile action="delete" file="#absGalleryPath##Name#" />
	</cfif>
</cfloop>

<!---Delete thumbnails.--->
<cfdirectory name="thumbnailsDir" action="list" 
			directory="#absThumbnailsPath#" filter="*">
<cfloop query="thumbnailsDir"> 
	<cfif FileExists("#absThumbnailsPath##Name#")>
		<cffile action="delete" file="#absThumbnailsPath##Name#" />
	</cfif>
</cfloop>
		
<!---NOTE: If you do not want to delete previously uploaded files, just 
remove or comment out the code above.--->

<!---Create XML file which will keep information about files (image dimensions, description, etc). 
XML is used solely for brevity. In real-life application most likely you will use database instead.--->
<cfset descriptions=XmlParse("<files />") />
	
<!---Get total number of uploaded files (all files are uploaded in a single package) and
iterate through uploaded data and save the original file, thumbnail, and description.--->
<cfloop index="i" from="1" to="#Form.FileCount#">

	<!---Get source file and save it to disk.--->
	<cffile action="UPLOAD" filefield="SourceFile_#i#" 
		destination="#absGalleryPath#" 
		nameconflict="MakeUnique">
		
	<cfset fileName="#serverFile#">

	<!---Get first thumbnail (the single thumbnail in this code sample) and save it to disk.--->
	<cffile action="UPLOAD" filefield="Thumbnail1_#i#" 
		destination="#absThumbnailsPath#" 
		nameconflict="MakeUnique">

	<!---Rename thumbnail file so that it has .jpg extension--->
	<cffile action="rename"
		source="#absThumbnailsPath#/#serverFile#"
		destination="#absThumbnailsPath#/#fileName#.jpg"> 

	<!---Save file info.--->
	<cfset xmlFile=XmlElemNew(descriptions, "file") />
	<cfset xmlFile.XmlAttributes.name=fileName />
	<cfset xmlFile.XmlAttributes.width=evaluate("Form.Width_#i#") />
	<cfset xmlFile.XmlAttributes.height=evaluate("Form.Height_#i#") />
	<cfset xmlFile.XmlAttributes.title=evaluate("Form.Title_#i#") />	
	<cfset xmlFile.XmlAttributes.description=evaluate("Form.Description_#i#") />
	<cfset ArrayAppend(descriptions.files.XmlChildren, xmlFile) />	

</cfloop>

<cffile action="write" file="#absGalleryPath#Descriptions.xml" 
		output="#toString(descriptions)#" />

