cfimage won't let go of my file so I can't delete it.
I am designing a client image uploader that will also process the image—resize, sharpen, etc. Right now I am just working out the uploading bugs, namely if the client wants to replace the image.
It appears that, once uploaded, ColdFusion is keeping the file in memory, so I can't delete or overwrite it until some undefined time afterwards (presumably once the file is kicked out of memory). I am not sure how to get ColdFusion to release the file so that it can be deleted.
You can see I have a try/catch statement; this is where the code is erroring out. Sometimes, if I click the refresh link inside the catch statement it will finish through the rest of the code, but most of the time, it just errors out.
If I upload an image for one event, move to a different event and upload an image for that event, and come back to the first one, most of the time I can replace it, but I really don't think it's unreasonable to upload a file and then immediately replace it (e.g. if the user accidentally uploaded the wrong image the first time).
<!-- Add image -->
<!-- #event_id# is passed in from the URL -->
<cfset graphicPath = expandPath("./graphics")>
<cfset thumbWidth = 100>
<cfset thumbHeight = 65>
<cfset fileName = "">
<cfif structKeyExists(form,"fileUpload") and len(form.fileUpload)>
<!-- Upload the file; if one already exists with the same file name, make it unique (we will be renaming it, anyway). -->
<cffile action="upload"
filefield="fileUpload"
destination="#graphicPath#\"
nameconflict="makeUnique"
>
<!-- Read the new image. -->
<cfimage
action="read"
source="#graphicPath#\#file.serverFile#"
name="uploadedImage"
>
<!-- If the file already exists, delete it so that there are no conflicts. -->
<cfif fileExists("#graphicPath#\#event_id#.jpg")>
<cftry>
<cffile action="delete"
file="#graphicPath#\#event_id#.jpg"
>
<cfcatch>
Image could not be replaced. <a href="javascript:location.reload(true);" title="Refresh the Page">Try again?</a>
<cffile action="delete"
file="#graphicPath#\#file.serverFile#"
>
<cfabort>
</cfcatch>
</cftry>
</cfif>
<!-- Rename the file so it can be accessed with the database later. -->
<cffile action="rename"
source="#graphicPath#\#file.serverFile#"
destination="#graphicPath#\#event_id#.jpg"
>
<cfset fileName = #event_id# & ".jpg">
<!--- Read the image details. --->
<cfimage
action="info"
source="#graphicPath#\#fileName#"
structname="imageFile"
/>
<!--- Figure out which way to scale the image. --->
<cfif uploadedImage.width gt uploadedImage.height>
<cfset thumbPercent = (thumbWidth / uploadedImage.width)>
<cfelse>
<cfset thumbPercent = (thumbHeight / uploadedImage.height)>
</cfif>
<!--- Calculate the new thumbnail and image height/width. --->
<cfset thumbWidth = round(uploadedImage.width * thumbPercent)>
<cfset thumbHeight = round(uploadedImage.height * thumbPercent)>
<!--- Create a thumbnail for the image. --->
<cfimage
action="resize"
source="#graphicPath#/#fileName#"
height="#thumbHeight#"
width="#thumbWidth#"
destination="#graphicPath#/thumbs/#fileName#"
overwrite="true"
/>
</cfif>
Let me know if you have any questions.
