Listing 1

<!---
CUSTOM TAG: UPLOADHELPER
(C)2003 Randy L. Smith, MCPI, LLC http://www.mcpi.com randy@mcpi.com 10/15/2003
Permission is granted for free, uninhibited use by all with kudos &
thanks to the crew at Macromediate Cold Fusion Developers Exchange!
(http://webforums.macromedia.com/coldfusion/)

Purpose of tag is to process image uploads with sensitivity to Mac's.
The attributes used here make this tag very generic. Adjust appropriately
for your needs.

Attributes:
FILETYPE is either "IMAGE" (default) or "DOCUMENT"
DELETEFIELD is the field name of the check box for the delete option; if defined,
delete = YES
SERVERPATH is the path to where the images are stored relative to the server;
DO NOT INCLUDE THE ENDING SLASH ON THE SERVER PATH!
PHOTOFIELD is the field name where the image file name is stored
PHOTOVALUE is the actual name of the image file
PHOTOALIGNFIELD is the field name where the alignment information is stored
PHOTOALIGNVALUE is the alignment value to set (RIGHT=default)
PHOTOCAPTIONFIELD is the field name where the image caption is stored
PHOTOCAPTIONVALUE is the caption value to set
IDENTITYFIELD is the name of the identity or identifying field
IDENTITYVALUE is the value to search the IDENTITYFIELD field for to find record
DATASOURCE is the DSN to the database
USERNAME is the username for the database, if needed
PASSWORD is the password for the database, if needed
TABLENAME is the table where the record of the image is stored
USEIMAGESIZE YES|NO : YES will use the code from the IMAGESIZE tag if no
extension is found on the file. If the file is an acceptable file type, it will put the extension on the file for you and not error. If you use this option, you will also receive the variables of PhotoWidth, PhotoHeight, and PhotoType as provided by the
imagesize code.

SPECIAL NOTE: USEIMAGESIZE="YES" will also REJECT anything that is not in the
JPG or GIF family of images! The error messages have been adjusted to account for that. Depending on your needs, you might want to modify this code to ALWAYS check the type to make sure of what is being uploaded to your server. That would trim the size of this tag quite a bit!
RECOMMENDED: Review CFX_IMAGE by Jukka Manner before you do this. Excellent tag!
Returns:
PhotoUploadError =
-1 if false attempt and processing should be ignored; see near-bottom of
this script for more information.
0 if successful
1 if there was an upload error
2 if some element was missing and request could not be completed
PhotoErrorMessage = the reason for the error
PhotoWidth, PhotoHeight, and PhotoType if USEIMAGESIZE is YES.

Example call:
CF_UPLOADHELPER
FILETYPE="IMAGE"
DELETEFIELD="killphoto1"
SERVERPATH="c:clientfilesittybittycorpuploadedimages"
PHOTOFIELD="photo1"
PHOTOVALUE="bluehills.jpg"
PHOTOALIGNFIELD="photo1align"
PHOTOALIGNVALUE="LEFT"
PHOTOCAPTIONFIELD="photo1caption"
PHOTOCAPTIONVALUE="The Blue Hills"
IDENTITYFIELD="itemid"
IDENTITYVALUE="14"
DATASOURCE="clientinfo"
USERNAME="jdoe"
PASSWORD="sd8^53"
TABLENAME="clientimages"
ACCEPT_TYPE="image/jpeg,image/jpg,image/pjpeg"
ALLOWED_EXT=".jpeg,.jpg,.jpe,.pjpeg"
USEIMAGESIZE="YES"

FYI: On the error messages, "INT" refers to Internal and the number represents a
close approximation of the line number. It's a lot quicker to zero in on problems
or explain to them what's happening when the customer unwittingly gives you the
line number!
--->

<cfparam name="ATTRIBUTES.FILETYPE" default="IMAGE">
<cfparam name="ATTRIBUTES.DELETEFIELD" default="">
<cfparam name="ATTRIBUTES.SERVERPATH" default="">
<cfparam name="ATTRIBUTES.PHOTOFIELD" default="">
<cfparam name="ATTRIBUTES.PHOTOVALUE" default="">
<cfparam name="ATTRIBUTES.PHOTOALIGNFIELD" default="">
<cfparam name="ATTRIBUTES.PHOTOALIGNVALUE" default="RIGHT">
<cfparam name="ATTRIBUTES.PHOTOCAPTIONFIELD" default="">
<cfparam name="ATTRIBUTES.PHOTOCAPTIONVALUE" default="">
<cfparam name="ATTRIBUTES.IDENTITYFIELD" default="">
<cfparam name="ATTRIBUTES.IDENTITYVALUE" default="">
<cfparam name="ATTRIBUTES.DATASOURCE" default="">
<cfparam name="ATTRIBUTES.USERNAME" default="">
<cfparam name="ATTRIBUTES.PASSWORD" default="">
<cfparam name="ATTRIBUTES.TABLENAME" default="">
<cfparam name="ATTRIBUTES.USEIMAGESIZE" default="NO">
<cfparam name="ATTRIBUTES.ACCEPT_TYPE" default="">
<cfparam name="ATTRIBUTES.ALLOWED_EXT" default="">

<!--- Check to make sure we meet the bare minimums --->
<cfif LEN(TRIM(ATTRIBUTES.SERVERPATH)) LT 1>
<cfset CALLER.PhotoUploadError=2>
<cfset CALLER.PhotoErrorMessage=TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD) &
" UPLOAD ERROR: Server path must be supplied in SERVERPATH attribute.">
<cfelseif LEN(TRIM(ATTRIBUTES.PHOTOFIELD)) LT 1>
<cfset CALLER.PhotoUploadError=2>
<cfset CALLER.PhotoErrorMessage=TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD) &
" UPLOAD ERROR: Field name of photo/image must be supplied in PHOTOFIELD attribute.">
<cfelseif LEN(TRIM(ATTRIBUTES.IDENTITYFIELD)) LT 1>
<cfset CALLER.PhotoUploadError=2>
<cfset CALLER.PhotoErrorMessage=TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD) &
" UPLOAD ERROR: Identity field must be supplied in IDENTITYFIELD attribute.">
<cfelseif LEN(TRIM(ATTRIBUTES.IDENTITYVALUE)) LT 1>
<cfset CALLER.PhotoUploadError=2>
<cfset CALLER.PhotoErrorMessage=TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD) &
" UPLOAD ERROR: Identity value must be supplied in IDENTITYVALUE attribute.">
<cfelseif LEN(TRIM(ATTRIBUTES.DATASOURCE)) LT 1>
<cfset CALLER.PhotoUploadError=2>
<cfset CALLER.PhotoErrorMessage=TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD) &
" UPLOAD ERROR: Datasource name must be supplied in DATASOURCE attribute.">
<cfelseif LEN(TRIM(ATTRIBUTES.TABLENAME)) LT 1>
<cfset CALLER.PhotoUploadError=2>
<cfset CALLER.PhotoErrorMessage=TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD) &
" UPLOAD ERROR: Table name must be supplied in TABLENAME attribute.">
<cfelse>

<!--- Modify the following lists to suit your needs. One version of list available at http://www.asahi-net.or.jp/en/guide/cgi/mimetype.html --->
<cfif TRIM(ATTRIBUTES.FILETYPE) IS NOT "IMAGE">
<cfif LEN(TRIM(ATTRIBUTES.ACCEPT_TYPE)) GT 0>
<cfset ACCEPT_TYPE=#TRIM(ATTRIBUTES.ACCEPT_TYPE)#>
<cfelse>
<cfset ACCEPT_TYPE="application/rtf,application/msword,application/pdf,application/
wordperfect5.1,application/x-excel,application/x-stuffit,application/x-tar,application/
zip,text/css,text/html,text/plain,text/richtext,text/tab-separated-values,
text/xml,text/xsl">
</cfif>
<cfif LEN(TRIM(ATTRIBUTES.ALLOWED_EXT)) GT 0>
<cfset ALLOWED_EXT=#TRIM(ATTRIBUTES.ALLOWED_EXT)#>
<cfelse>
<cfset ALLOWED_EXT=".rtf,.pdf,.xls,.sit,.tar,.tgz,.zip,.css,.htm,.hmtl,.txt,.rtx,
.tsv,.xml,.xsl,.doc">
</cfif>
<cfelse>
<cfif LEN(TRIM(ATTRIBUTES.ACCEPT_TYPE)) GT 0>
<cfset ACCEPT_TYPE=#TRIM(ATTRIBUTES.ACCEPT_TYPE)#>
<cfelse>
<cfset ACCEPT_TYPE="image/gif,image/jpeg,image/jpg,image/pjpeg,image/x-png,image,video/
mpeg,video/quicktime,video/x-msvideo,video/x-sgi-movie,application/mspowerpoint,
application/x-shockwave-flash,image/bmp,image/png">
</cfif>
<cfif LEN(TRIM(ATTRIBUTES.ALLOWED_EXT)) GT 0>
<cfset ALLOWED_EXT=#TRIM(ATTRIBUTES.ALLOWED_EXT)#>
<cfelse>
<cfset ALLOWED_EXT=".gif,.jpeg,.jpg,.jpe,.mpeg,.mpg,.mpe,.qt,.mov,.avi,.movie,.pot,.pps,
.ppt,.ppz,.swf,.bmp,.png,.pjpeg">
</cfif>
</cfif>

<!--- Format allowed_ext so they look better on the error messages. Do not put spaces in above list as Mac's will not find the extension --->
<cfset Formatted_Allowed_Ext="">
<cfset AlmostTheEnd=LISTLEN(Allowed_Ext)-1>
<cfloop index="ii" from="1" to="#LISTLEN(ALLOWED_EXT)#">
<cfset Formatted_Allowed_Ext=Formatted_Allowed_Ext & " " & #TRIM(LISTGETAT(Allowed_Ext,ii))#>
<cfif ii LT AlmostTheEnd>
<cfset Formatted_Allowed_Ext=Formatted_Allowed_Ext & ", ">
<cfelseif ii IS AlmostTheEnd>
<cfset Formatted_Allowed_Ext=Formatted_Allowed_Ext & ", and ">
</cfif>
</cfloop>

<cfset CALLER.PhotoUploadError=0>
<cfset CALLER.PhotoErrorMessage="">
<cfset DefaultErrorMessage="Your file was NOT uploaded. The usual cause
of a file upload error is because your file does not have an extension on it
(most common in the Apple/Macintosh computers). Please ensure it has the appropriate extension.
Allowed extensions include " & TRIM(Formatted_Allowed_Ext)>

<!---
If user directs present file to be deleted AND uploads a new file, we can save
ourselves a little CPU time by not duplicating our efforts. We'll keep track with
a flag.
Same with updating caption and alignment.
--->
<cfset FileAlreadyDeletedFlag=0>
<cfset CaptionAlignmentWrittenFlag=0>
<cfset CALLER.PhotoWidth=-1>
<cfset CALLER.PhotoHeight=-1>
<cfset CALLER.PhotoType="NULL">

<cfif LEN(TRIM(ATTRIBUTES.DELETEFIELD)) GT 0 AND ISDEFINED(ATTRIBUTES.DELETEFIELD)>
<cfquery name="FindPhoto" datasource="#TRIM(ATTRIBUTES.DATASOURCE)#"
username="#TRIM(ATTRIBUTES.USERNAME)#" password="#TRIM(ATTRIBUTES.PASSWORD)#">
SELECT #TRIM(ATTRIBUTES.PHOTOFIELD)# AS ThePhoto
FROM #TRIM(ATTRIBUTES.TABLENAME)#
WHERE #TRIM(ATTRIBUTES.IDENTITYFIELD)#=#ATTRIBUTES.IDENTITYVALUE#
</cfquery>
<cfif FindPhoto.RecordCount GT 0 AND FILEEXISTS("#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(FindPhoto.ThePhoto)#")>
<cfset TempError=0>
<cftry>
<cffile action="delete" file="#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(FindPhoto.ThePhoto)#">
<cfcatch type="any">
<!--- This is not a fatal error so do not set uploaderror variable
but do let us manage the messages --->
<cfset TempError=1>
<cfset CALLER.PhotoErrorMessage=TRIM(FindPhoto.ThePhoto) & " was
found on the server, but it could
not be deleted for some reason (file lock or other system
problem). Your webmaster would appreciate
if if you passed this message along to them. [INT:171]">
</cfcatch>
</cftry>
<cfif TempError IS NOT 1>
<cfset CALLER.PhotoErrorMessage="Existing image was removed.">
</cfif>
<!--- Regardless of error, don't try to delete it again. --->
<cfset FileAlreadyDeletedFlag=1>
<cfelse>
<cfset CALLER.PhotoErrorMessage=TRIM(TRIM(CALLER.PhotoErrorMessage) &
" File not found, could not delete it. Database corrected. [INT:180]")>
</cfif>

<!--- In either case, update the database by blanking out the file and caption
fields --->
<cfquery datasource="#TRIM(ATTRIBUTES.DATASOURCE)#"
username="#TRIM(ATTRIBUTES.USERNAME)#" password="#TRIM(ATTRIBUTES.PASSWORD)#">
UPDATE #TRIM(ATTRIBUTES.TABLENAME)# SET
<cfif LEN(TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)) GT 0>
#TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)#='',
</cfif>
#TRIM(ATTRIBUTES.PHOTOFIELD)# = ''
WHERE #TRIM(ATTRIBUTES.IDENTITYFIELD)#=#ATTRIBUTES.IDENTITYVALUE#
</cfquery>
<cfset CaptionAlignmentWrittenFlag=1>
</cfif>

<cfif LEN(TRIM(ATTRIBUTES.PHOTOVALUE)) GT 0 AND
FILEEXISTS("#TRIM(ATTRIBUTES.PHOTOVALUE)#")>
<CFFILE ACTION="ReadBinary" FILE="#TRIM(ATTRIBUTES.PHOTOVALUE)#"
VARIABLE="aBinaryObj">
<CFIF len(aBinaryObj) gt 2>
<cfif UCASE(TRIM(ATTRIBUTES.USEIMAGESIZE)) IS "YES">
<CFTRY>
<CFFILE
DESTINATION="#TRIM(ATTRIBUTES.SERVERPATH)#"
ACTION="UPLOAD"
nameconflict="MAKEUNIQUE"
FILEFIELD="#TRIM(ATTRIBUTES.PHOTOFIELD)#">
<cfcatch type="any">
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage=DefaultErrorMessage & "
[INT:207]">
</cfcatch>
</CFTRY>
<cfif CALLER.PhotoUploadError LT 1 AND ISDEFINED("FILE.ServerFile")>
<!--- Save the file name so we don't lose it on the next cffile
command --->
<cfset NewFile=File.ServerFile>
<cfset ReverseFileName=REVERSE(NewFile)>
<cfset PeriodLocation=FIND(".",ReverseFileName)>
<cfif PeriodLocation GT 0>
<cfset
TheExtension=REVERSE(LEFT(ReverseFileName,PeriodLocation))>
<cfif NOT LISTFINDNOCASE(Allowed_Ext,TheExtension)>
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage=DefaultErrorMessage & "
[INT:219:#TheExtension#]">
</cfif>
<cfelse>
<cfif FileExists("#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(NewFile)#")>
<!---
NOTE: The following code was unabashedly "stolen" from the IMAGESIZE tag. This tag is
no longer available on the Macromedia Exchange and the FuseWare web site is under
construction with no mention in the code or on the web site on how to reach them. There is no copyright information in the IMAGESIZE tag. This tag, UploadHelper, is being
distributed free with no copyright or restrictions (read: I am not making anything
off of this, as the authors of IMAGESIZE were not charging for theirs, either). So,
it is hereby duly noted that the following section of code was authored by FuseWare
and I am not claiming it as my own. I offer it here in the spirit of helping others.
I added new error handling code where appropriate.
--->
<cffile action="READ" file="#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(NewFile)#" variable="Gif">
<CFSET Type_JPG="ÿØÿà">
<CFSET Type_GIF="GIF8">
<CFSET Header="">
<CFSET ImageType=Left(GIF,4)>
<CFIF ImageType is Type_JPG>
<CFSET MYString="">
<CFSET POSITION = 1>
<CFLOOP Condition="Position LE Len(GIF)">
<CFSET C=Mid(Gif,Position,1)>
<CFIF (ASC(C) is 218) >
<CFBREAK>
</CFIF>
<CFSET Header=ListAppend(Header,ASC(C))>
<CFSET POSITION = Position +1>
</cfloop>

<CFSET Position=0>
<CFSET POSITION = Find("255,192",Header)>
<CFIF Position is 0>
<CFSET POSITION = Find("255,193",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,194",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,195",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,196",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,197",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,198",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,199",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,201",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,202",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,203",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,205",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,206",Header)>
</cfif>
<CFIF Position is 0>
<CFSET POSITION = Find("255,207",Header)>
</cfif>
<CFIF Position is 0>
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage="Your file was NOT uploaded. The usual cause of a file upload error is because your file does not have an extension on it (most common in the Apple/Macintosh computers). Please ensure it has the appropriate extension. Allowed extensions include the JPG and GIF family. [INT:297:POSITION_ERROR]">
<CFELSE>
<CFSET Header=Mid(Header,POSITION,Len(Header) - POsition)>
<CFSET CompressionType=ListGetAt(Header,2)>
<CFSET Length=ListGetAt(Header,3) & " " & ListGetAt(Header,4)>
<CFSET Caller.Type=ListGetAt(Header,5)>
<CFSET Caller.Height=(ListGetAt(Header,6)*256) + ListGetAt(Header,7)>
<CFSET Caller.Width=(ListGetAt(Header,8)*256) + ListGetAt(Header,9)>
</cfif>
<CFELSEIF ImageType is Type_GIF>
<CFSET Caller.Width=(ASC(Mid(Gif, 7, 1)) + (ASC(Mid(Gif, 8, 1)))*256)>
<CFSET Caller.Height=(ASC(Mid(Gif, 9, 1)) + (ASC(Mid(Gif, 10, 1)))*256)>
<CFSET Caller.Type=Mid(Gif, 1, 6)>
<CFELSE>
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage="Your file was NOT uploaded. The usual cause of a file upload error is because your file does not have an extension on it
(most common in the Apple/Macintosh computers). Please ensure it has the appropriate extension. Allowed extensions include the JPG and GIF family. [INT:315:IMAGE_TYPE_ERROR]">
</cfif>
<!--- End of ImageSize error code --->

<cfif LISTFINDNOCASE(Allowed_Ext,CALLER.PhotoType)>
<cfset NewFile=TRIM(NewFile) & "." & TRIM(CALLER.PhotoType)>
<cftry>
<cffile action="rename" destination="#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(NewFile)#"
source="#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(FILE.ServerFile)#">
<cfcatch type="any">
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage="Your file was uploaded but we were unable to rename it to add the proper extension, so this image will be deleted. Please rename your local file with a proper and valid extension and try again. Allowed extensions include " & TRIM(Formatted_Allowed_Ext) & " [INT:330]">
</cfcatch>
</cftry>
<cfif CALLER.PhotoUploadError GT 0>
<cftry>
<cffile action="delete"
file="#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(FILE.ServerFile)#">
<cfcatch type="any">
<cfset CALLER.PhotoErrorMessage=" DELETION ATTEMPT FAILED! PLEASE NOTE that this image will very likely cause problems. Please contact your webmaster immediately. Leave this message on display or copy/paste it into an e-mail message to send to them. [INT:339]">
</cfcatch>
</cftry>
</cfif>
<cfelse>
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage=DefaultErrorMessage & "
[INT:345:#CALLER.PhotoType#]">
</cfif>
<cfelse>
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage=DefaultErrorMessage & "
[INT:349]">
</cfif>
</cfif>
</cfif>
<cfelse>
<CFTRY>
<CFFILE
DESTINATION="#TRIM(ATTRIBUTES.SERVERPATH)#"
ACTION="UPLOAD"
nameconflict="MAKEUNIQUE"
FILEFIELD="#TRIM(ATTRIBUTES.PHOTOFIELD)#"
ACCEPT="#ACCEPT_TYPE#">
<cfcatch type="any">
<cfset CALLER.PhotoUploadError=1>
<cfset CALLER.PhotoErrorMessage=DefaultErrorMessage & "
[INT:363]">
</cfcatch>
</CFTRY>
<cfif CALLER.PhotoUploadError LT 1 AND ISDEFINED("FILE.ServerFile")>
<!--- Save the file name so we don't lose it on the next cffile
command --->
<cfset NewFile=File.ServerFile>
</cfif>
</cfif>

<!--- Can only continue if no errors have been caught so far --->
<cfif CALLER.PhotoUploadError LT 1>
<!---
If there was an existing file, delete it -- if it hasn't been already
-- to prevent server bloat from orphaned files
--->
<cfif FileAlreadyDeletedFlag IS NOT 1>
<cfquery name="FindPhoto" datasource="#TRIM(ATTRIBUTES.DATASOURCE)#"
username="#TRIM(ATTRIBUTES.USERNAME)#"
password="#TRIM(ATTRIBUTES.PASSWORD)#">
SELECT #TRIM(ATTRIBUTES.PHOTOFIELD)# AS ThePhoto
FROM #TRIM(ATTRIBUTES.TABLENAME)#
WHERE #TRIM(ATTRIBUTES.IDENTITYFIELD)#=#ATTRIBUTES.IDENTITYVALUE#
</cfquery>
<cfif FindPhoto.RecordCount GT 0 AND FILEEXISTS("#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(FindPhoto.ThePhoto)#")>
<cfset TempError=0>
<cftry>
<cffile action="delete"
file="#TRIM(ATTRIBUTES.SERVERPATH)##TRIM(FindPhoto.ThePhoto)#">
<cfcatch type="any">
<!--- This is not a fatal error so do not set uploaderror variable but do let us manage the messages --->
<cfset TempError=1>
<cfset CALLER.PhotoErrorMessage=TRIM(FindPhoto.ThePhoto) & " was found on the server, but it could not be deleted for some reason (file lock or other system problem). Your webmaster would appreciate if if you passed this message along to them. [INT:393]">
</cfcatch>
</cftry>
<cfif TempError IS NOT 1>
<cfset CALLER.PhotoErrorMessage="Existing image was removed.">
</cfif>
</cfif>
</cfif>

<!--- Now update the database with the new file information --->
<cfquery datasource="#TRIM(ATTRIBUTES.DATASOURCE)#"
username="#TRIM(ATTRIBUTES.USERNAME)#"
password="#TRIM(ATTRIBUTES.PASSWORD)#">
UPDATE #TRIM(ATTRIBUTES.TABLENAME)# SET
<cfif LEN(TRIM(ATTRIBUTES.PHOTOALIGNFIELD)) GT 0>
#TRIM(ATTRIBUTES.PHOTOALIGNFIELD)#='#TRIM(ATTRIBUTES.PHOTOALIGNVALUE)#',
</cfif>
<cfif LEN(TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)) GT 0>
#TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)#='#TRIM(ATTRIBUTES.PHOTOCAPTIONVALUE)#',
</cfif>
#TRIM(ATTRIBUTES.PHOTOFIELD)# = '#TRIM(NewFile)#'
WHERE #TRIM(ATTRIBUTES.IDENTITYFIELD)#=#ATTRIBUTES.IDENTITYVALUE#
</cfquery>
<cfset CaptionAlignmentWrittenFlag=1>
<cfset CALLER.PhotoErrorMessage=TRIM(TRIM(CALLER.PhotoErrorMessage) & " "
& TRIM(ATTRIBUTES.PHOTOVALUE)) & " was uploaded successfully.">
</cfif>
<cfelse>
<!--- This error is ignored. Some versions of Mac will pass a field name variable whether you BROWSEd to a file on your hard drive or not. The READBINARY test checks to see if this was a valid file to look for and if it doesn't pass this simple test, we can ignore this as a request that the user never made in the first place.
--->
<cfset CALLER.PhotoUploadError=-1>
</cfif>
</cfif>
</cfif>

<!--- Don't let caption and alignment changes go undone if there are no errors --->
<cfif CALLER.PhotoUploadError LT 1
AND CaptionAlignmentWrittenFlag IS 0
AND (LEN(TRIM(ATTRIBUTES.PHOTOALIGNFIELD)) GT 0 OR
LEN(TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)) GT 0)>
<cfquery datasource="#TRIM(ATTRIBUTES.DATASOURCE)#"
username="#TRIM(ATTRIBUTES.USERNAME)#" password="#TRIM(ATTRIBUTES.PASSWORD)#">
<cfif LEN(TRIM(ATTRIBUTES.PHOTOALIGNFIELD)) GT 0>
UPDATE #TRIM(ATTRIBUTES.TABLENAME)#
SET #TRIM(ATTRIBUTES.PHOTOALIGNFIELD)#='#TRIM(ATTRIBUTES.PHOTOALIGNVALUE)#'
WHERE #TRIM(ATTRIBUTES.IDENTITYFIELD)#=#ATTRIBUTES.IDENTITYVALUE#
</cfif>
<cfif LEN(TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)) GT 0>
UPDATE #TRIM(ATTRIBUTES.TABLENAME)#
SET #TRIM(ATTRIBUTES.PHOTOCAPTIONFIELD)#='#TRIM(ATTRIBUTES.PHOTOCAPTIONVALUE)#'
WHERE #TRIM(ATTRIBUTES.IDENTITYFIELD)#=#ATTRIBUTES.IDENTITYVALUE#
</cfif>
</cfquery>
<cfset CALLER.PhotoErrorMessage=TRIM(TRIM(CALLER.PhotoErrorMessage) & " Updates
successful.")>
</cfif>

Listing 2

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!---
Simple script for testing the UPLOADHELPER tag.
Uses table with at least the following field elements:
itemid INT PRIMARY IDENTITY
photo1 VARCHAR(500) 100 is probably sufficient
photo1caption VARCHAR(50)
photo1align VARCHAR(5) Could be BIT unless additional image alignment
options desired
--->
<html>
<head>
<title>Testing CF_UPLOADHELPER</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<!--- Accept form parameters, set defaults as needed --->
<cfparam name="itemid" default="-1">
<cfparam name="option" default="">
<cfparam name="photo1" default="">
<cfparam name="photo1align" default="RIGHT">
<cfparam name="killphoto1" default="">

<!--- Establish common variables --->
<cfset IMG_Server_Path="c:sharedclientfilesuploadedimages">
<cfset IMG_URL_Path="http://shared.myshareddomain.com/uploadedimages">
<cfset TheDatasource="mydatasource">
<cfset TheUsername="mydbusername">
<cfset ThePassword="mydbpassword">
<cfset TheTableName="tblMCPI_tablenamehere">
<cfset ThisScript="ImgTagtest.cfm">
<cfset TheFileType="IMAGE">

<cfif TRIM(TheFileType) IS NOT "IMAGE">
<cfset ACCEPT_TYPE="application/rtf,application/msword,application/pdf,application/
wordperfect5.1,application/x-excel,application/x-stuffit,application/x-tar,
application/zip,text/css,text/html,text/plain,text/richtext,text/
tab-separated-values,text/xml,text/xsl">
<cfset ALLOWED_EXT=".rtf,.pdf,.xls,.sit,.tar,.tgz,.zip,.css,.htm,.hmtl,
.txt,.rtx,.tsv,.xml,.xsl,.doc">
<cfelse>
<cfset ACCEPT_TYPE="image/gif,image/jpeg,image/jpg,image/pjpeg,image/x-png,image,video/
mpeg,video/quicktime,video/x-msvideo,video/x-sgi-movie,application/mspowerpoint,
application/x-shockwave-flash,image/bmp,image/png">
<cfset ALLOWED_EXT=".gif,.jpeg,.jpg,.jpe,.mpeg,.mpg,.mpe,.qt,.mov,.avi,.movie,
.pot,.pps,.ppt,.ppz,.swf,.bmp,.png,.pjpeg">
</cfif>


<body>
<cfswitch expression="#UCASE(TRIM(option))#">
<cfcase value="EDIT">
<cfquery name="EditItem" datasource="#TheDatasource#" username="#TheUsername#"
password="#ThePassword#">
SELECT itemid, photo1, photo1caption, photo1align
FROM #TheTableName#
WHERE itemid=#itemid#
</cfquery>
<cfform action="#ThisScript#" method="POST" enablecab="No"
enctype="multipart/form-data">
<table border="0" cellspacing="2" cellpadding="2" align="center">
<!---
The following TR block could be made into a custom tag using the format you normally
create your forms in. It would help to keep your image requests consistent.
--->
<tr >
<th align="left" valign="top">Photo/Image:
<!---
Provide thumbnail preview of photo so user knows exactly which image
they are editing or deleting. Only can delete if file really exists.
--->
<font style="font-size: 11;">
<cfif LEN(TRIM(EditItem.photo1)) GT 0 AND
FILEEXISTS("#IMG_Server_Path##EditItem.photo1#")>
<cfoutput>
<br><img src="#IMG_URL_Path#/#EditItem.photo1#" width="80" border="0"
align="right">
<br clear="all"><input name="KillPhoto1" type="checkbox"
value="#EditItem.photo1#">Delete
</cfoutput>
<cfelse>
<br><br>&nbsp;&nbsp;(no photo)
</cfif>
</font>
</th>
<td align="left" valign="top" width="400">
<!--- Form must have 'enctype="multipart/form-data"' in order for this to
work, Mac or not! --->
<INPUT TYPE="file" NAME="photo1" ACCEPT="#TRIM(ACCEPT_TYPE)#" size="40"
maxlength="500">
<font style="font-size: 11;">
<br><strong>CAPTION:</strong> <cfinput name="photo1caption" type="text"
value="#TRIM(EditItem.photo1caption)#" size="30" maxlength="50"
required="no">
<br><strong>ALIGNMENT:</strong>
<cfif TRIM(EditItem.photo1align) IS "LEFT">
<input name="photo1align" type="radio" value="LEFT" checked>Left
&nbsp;&nbsp;&nbsp;&nbsp;
<input name="photo1align" type="radio" value="RIGHT">Right
<cfelse>
<input name="photo1align" type="radio" value="LEFT">Left
&nbsp;&nbsp;&nbsp;&nbsp;
<input name="photo1align" type="radio" value="RIGHT" checked>Right
</cfif>
<br>Browse to select a photo or image from your hard drive. Caption is optional.
<br>Only the following extensions are allowed: <cfoutput>#ALLOWED_EXT#</cfoutput>
</font>
</td>
</tr>
<tr>
<td align="center" valign="middle" colspan="2">
<cfoutput>
<input type="hidden" name="itemid" value="#itemid#">
</cfoutput>
<input type="submit" name="option" value="Update">
</td>
</tr>
</table>
</cfform>
</cfcase>

<cfcase value="UPDATE">
<cf_UploadHelper
FILETYPE="#TRIM(TheFileType)#"
DELETEFIELD="killphoto1"
SERVERPATH="#TRIM(IMG_Server_Path)#"
PHOTOFIELD="photo1"
PHOTOVALUE="#TRIM(photo1)#"
PHOTOALIGNFIELD="photo1align"
PHOTOALIGNVALUE="#TRIM(photo1align)#"
PHOTOCAPTIONFIELD="photo1caption"
PHOTOCAPTIONVALUE="#TRIM(photo1caption)#"
IDENTITYFIELD="itemid"
IDENTITYVALUE="#itemid#"
DATASOURCE="#TRIM(TheDatasource)#"
USERNAME="#TRIM(TheUsername)#"
PASSWORD="#TRIM(ThePassword)#"
TABLENAME="#TRIM(TheTableName)#"
USEIMAGESIZE="YES">

<cfoutput>
<h2>Test Complete</h2>
<!--- Output returned message, if any (if you want to) --->
<h3>CODE: #PhotoUploadError#</h3>
<cfif LEN(TRIM(PhotoErrorMessage)) GT 0>
<h3>MESSAGE: #PhotoErrorMessage#</h3>
</cfif>
<cfif PhotoUploadError LT 1 AND PhotoWidth GT 0>
<h3>WIDTH: #PhotoWidth#</h3>
<h3>HEIGHT: #PhotoHeight#</h3>
<h3>TYPE: #PhotoType#</h3>
</cfif>
<p><a href="#ThisScript#">Start Over</a></p>
</cfoutput>
</cfcase>

<cfdefaultcase>
<cfquery name="ListItems" datasource="#TheDatasource#" username="#TheUsername#"
password="#ThePassword#">
SELECT itemid, photo1, photo1caption, photo1align
FROM #TheTableName#
</cfquery>
<p>
<strong>SELECT IMAGE TO EDIT:</strong>
<ul>
<cfoutput query="ListItems">
<li><a href="#ThisScript#?option=EDIT&itemid=#itemid#">#TRIM(photo1caption)#
#TRIM(photo1)#</a></li>
</cfoutput>
</ul>
</p>
</cfdefaultcase>
</cfswitch>
</body>
</html>