Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading Multiple Files and storing their file names in a database using CFFILE and File.ServerFile

CFFILE

Uploading Multiple Files and storing their file names in a database using CFFILE and File.ServerFile

by  Bossmojoman  Posted    (Edited  )
Ok this may be a rough workaround but this allows you to use multiple <input type="file" name="aFile" > boxes in a form and not only upload them but write their filenames to a database.

First the Easy part: your form should look something like this

<form action="uploadgallery.cfm" method="post" enctype="multipart/form-data">
<input type="file" name="aFile" required="yes">
<input type="file" name="bFile" required="yes">
<input type="file" name="cFile" required="yes>
<input type="submit" value="Upload">
</form>

Note the enctype="multipart/form-data" Portion of the Rag, this is required for uploading non HTML files

Next, in the file your form points to, you are going to need some CFFILE Tags, like these


<cfif #form.aFile# NEQ "">
<cffile action="upload" filefield="aFile" destination="#GetDirectoryFromPath(GetCurrentTemplatePath())#images" ACCEPT="image/jpg, image/pjpeg,image/gif,image/bmp, image/tiff" nameconflict="OVERWRITE">
<cfset file1 = #file.serverfile#>
</cfif>

<cfif #form.bFile# NEQ "">
<cffile action="upload" filefield="bFile" destination="#GetDirectoryFromPath(GetCurrentTemplatePath())#images" ACCEPT="image/jpg, image/pjpeg,image/gif,image/bmp, image/tiff" nameconflict="OVERWRITE">
<cfset file2 = #file.serverfile#>
</cfif>

<cfif #form.cFile# NEQ "">
<cffile action="upload" filefield="cFile" destination="#GetDirectoryFromPath(GetCurrentTemplatePath())#images" ACCEPT="image/jpg, image/pjpeg,image/gif,image/bmp,image/tiff" nameconflict="MAKEUNIQUE">
<cfset file3 = #file.serverfile#>
</cfif>

A quick break down of this code: First I check to see if file im passing is there or not. This is important because your app will crash if the CFFILE trys to upload a file that isnt there.

The the CFFILE then begins to do its work, Action, being what you want it to do with the File, filefield being the file variable its working with (in this case the name of the input tag from the calling page.) Destination being the directory to store the file in. Accept is very important if you want to limit the files the enduser can upload, My app allows the user to upload graphics to database for viewing in a screenshot gallery.

ACCEPT="image/jpg, image/pjpeg,image/gif,image/bmp,image/tiff" Make sure you put a comma between each mime type in your accept list

Lastly, Nameconflict tells the server what to do if duplicate filenames exist, OVERWRITE just replaces the file, MAKEUNIQUE gives the new uploaded file a new random name. SKIP doesnt upload the file.

The main problem we have now isthat the FILE.SERVERFILE is rewritten after every CFFILE tag, so if you want to store the file name in a database you can only store the filename of the last file uploaded. To get around this we store the FILE.SERVERFILE value in a variable after each CFFILE tag. Now if you need to store the filename you just store the variables you created instead of storing FILE.SERVERFILE directly.

I hope this helps people having problems with this, if you have any questions feel free to contact me at glen@headcasemods.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top