Hi All,

I've already sent this to Scott who asked the question but here was my
go at a custom tag for file upload.... feel free to hack it to
bits/comment on it's crapness but I think it is pretty much covering
everything that you are discussing.... if you use it and improve it,
please let me have a copy :-)

Also tests for a directory to put the file in (within your main upload
location) and createds it if there isn't one

Probably better ways of doing some of the stuff but I was fairly new to
CF at the time.  BTW developed on CF5


------------ Useage ------------ 

<cf_rgufileupload
        rguFileFieldList="elmImageUpload"
        rguFileMaxSizeKb="50"
        rguFileUploadLocation="#sIF_UploadFolderImages#"
        rguNameConflict="MAKEUNIQUE"
        rguFilePutInFolder="FormID#SESSION.iIF_FormID#"
>

------------ Start of custom tag ------------ 


<cfparam name="ATTRIBUTES.rguFileFieldList" default="">
<cfparam name="ATTRIBUTES.rguFileMaxSizeKb" default="5000">
<cfparam name="ATTRIBUTES.rguFileInvalidExtensions"
default="cfml,cfm,asp,shtml,php,cgi,jsp,exe">
<cfparam name="ATTRIBUTES.rguFileValidMimeTypes"
default="text/comma-seperated-values,text/plain,application/msword,appli
cation/pdf,application/rtf,application/mspowerpoint,application/x-visio,
application/excel,application/x-msexcel,application/x-compressed,applica
tion/x-zip-compressed,application/vnd.ms-excel,application/x-excel,appli
cation/zip,application/x-shockwave-flash,image/png,image/jpeg,image/gif"
>
<cfparam name="ATTRIBUTES.rguFileUploadLocation" default="">
<cfparam name="ATTRIBUTES.rguFilePutInFolder" default="">
<cfparam name="ATTRIBUTES.rguNameConflict" default="ERROR">

<!--- Set up parameters for file upload --->
<cfset CALLER.rguFileName = "">
<cfset CALLER.rguFileError = "">
<cfset CALLER.rguFileUploadVersion = "1.1">
<cfset lFileError = "">
<cfset lFileNames = "">
<cfset sFileName = "">
<cfset iFileSize = "">
<cfset sFileMimeType = "">
<cfset sFileClientExt = "">
<cfset lValidNameConfilict = "ERROR,SKIP,MAKEUNIQUE,OVERWRITE">

<!--- Check all the paramters --->
<cfif ATTRIBUTES.rguFileFieldList EQ "">
        <cfset lFileError  = ListAppend(lFileError,"You must specify at
least one form field that contains a file to upload")>
</cfif>

<cfif NOT IsNumeric(ATTRIBUTES.rguFileMaxSizeKb)>
        <cfset lFileError  = ListAppend(lFileError,"The maximum size of
the file in Kb must be numeric")>
</cfif>

<cfif ATTRIBUTES.rguFileUploadLocation EQ "">
        <cfset lFileError = ListAppend(lFileError,"You must specify an
upload location")>
</cfif>

<cfif ListFindNoCase(lValidNameConfilict,ATTRIBUTES.rguNameConflict,",")
EQ 0>
        <cfset lFileError = ListAppend(lFileError,"You must specify a
valid name conflict parameter (Valid: #lValidNameConfilict#)")>
</cfif>
<cfif lFileError GT "">
        <cfset CALLER.rguFileError = lFileError>
        <cfexit>
</cfif>

<!--- Test for the Base Upload directory --->
<cfif DirectoryExists(ATTRIBUTES.rguFileUploadLocation) EQ False>
        <cfset CALLER.rguFileError = ListAppend(CALLER.rguFileError,"The
upload location you have specified does not exist")>
        <cfexit>
</cfif>

<!--- Original Upload Folder exists, see if we need to put it in a
specific
folder and if so test for it --->
<cfif ATTRIBUTES.rguFilePutInFolder GT "">
        <cfif
DirectoryExists("#ATTRIBUTES.rguFileUploadLocation#\#ATTRIBUTES.rguFileP
utInFolder#") EQ False>
                <cftry>
                        <cfdirectory action="create"
directory="#ATTRIBUTES.rguFileUploadLocation#\#URLEncodedFormat(ATTRIBUT
ES.rguFilePutInFolder)#">
                        <cfcatch type="ANY">
                                <cfset CALLER.rguFileError =
ListAppend(CALLER.rguFileError,"Unable to create sub directory for file
upload")>
                                <cfexit>
                        </cfcatch>
                </cftry>
        </cfif>
        <cfset rguFileFullPath =
"#ATTRIBUTES.rguFileUploadLocation#\#URLEncodedFormat(ATTRIBUTES.rguFile
PutInFolder)#">
<cfelse>
        <cfset rguFileFullPath = "#ATTRIBUTES.rguFileUploadLocation#">
</cfif>

<!--- Now that the proper location has been established, can upload file
--->
<cfloop list="#ATTRIBUTES.rguFileFieldList#" index="sFileField"
delimiters=",">
        <cfif Evaluate("FORM.#sFileField#") GT "">
                <cfset bFileUploaded = True>
                <cftry>
                        <cffile action="upload" fileField="#sFileField#"
destination="#rguFileFullPath#"
nameConflict="#ATTRIBUTES.rguNameConflict#">
                        <cfcatch type="ANY">
                                <cfset bFileUploaded = False>
                        </cfcatch>
                </cftry>
                
                <cfif bFileUploaded EQ True>
                        <!--- File was uploaded ok, next get all the
info about the newly uploaded file --->
                        <cfset sFileName = cffile.serverFile>
                        <cfset iFileSize = ((cffile.fileSize)/1024)>
                        <cfset sFileMimeType = cffile.contentSubType &
"/" & cffile.contentSubType>
                        <cfset sFileClientExt = cffile.ClientFileExt>

                        <cfset bValidFile = True>
                        <!--- Now we need to validate it against passed
variables --->
                        <cfif
ListFindNoCase(ATTRIBUTES.rguFileValidMimeTypes,sFileMimeType) GT 0>
                                <cfset bValidFile = False>
                                <cfset lFileError =
ListAppend(lFileError,"The MIME type of the file '#sFileName#' is not
valid.'")>
                        </cfif>
                        <cfif
ListFindNoCase(ATTRIBUTES.rguFileInvalidExtensions,sFileClientExt) GT 0>
                                <cfset bValidFile = False>
                                <cfset lFileError =
ListAppend(lFileError,"The client extension of the file '#sFileName#' is
not a valid type.'")>
                        </cfif>
                        <cfif iFileSize GT ATTRIBUTES.rguFileMaxSizeKb>
                                <cfset bValidFile = False>
                                <cfset lFileError =
ListAppend(lFileError,"The file '#sFileName#' is #Int(iFileSize)#Kb and
the maximum allowed is #ATTRIBUTES.rguFileMaxSizeKb#Kb")>
                        </cfif>
        
                        <cfif bValidFile EQ False>
                                <!--- This was not a valid upload, try
and delete file then exit --->
                                <cftry>
                                        <cffile action="delete"
file="#rguFileFullPath#\#sFileName#">
                                        <cfcatch type="ANY">
                                                <!--- Who cares, its not
there anyway --->
                                        </cfcatch>
                                </cftry>
                        <cfelse>
                                <cfset lFileNames =
ListAppend(lFileNames, sFileName)>
                        </cfif>
                <cfelse>
                        <cfset lFileError = ListAppend(lFileError,"There
has been an error in uploading your file.  Please ensure that you have
selected one.")>
                </cfif>
        <cfelse>
                <!--- Must put in something as CF ignores empty list
elements --->
                <cfset lFileNames = ListAppend(lFileNames, "NOFILE")>
                <cfset lFileError = ListAppend(lFileError,"The file
upload field '#sFileField#' was empty")>
        </cfif>
</cfloop>

<!--- Watch for unspecified error --->
<cfif sFileName EQ "" AND lFileError EQ "">
        <cfset CALLER.rguFileError = ListAppend(CALLER.rguFileError,"An
unspecified error has occurred.")>
</cfif>

<cfset CALLER.rguFileName = lFileNames>
<cfset CALLER.rguFileError = lFileError>
<cfexit>

------------ End of custom tag ------------ 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq

Signup for the Fusion Authority news alert and keep up with the latest news in 
ColdFusion and related topics. 
http://www.fusionauthority.com/signup.cfm

                                Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
                                

Reply via email to