Skip to main content
Known Participant
October 28, 2009
Answered

cfimage won't let go of my file so I can't delete it.

  • October 28, 2009
  • 1 reply
  • 2051 views

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.

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    "ColdFusion could not delete the file C:\ [...] \graphics\192.jpg for an unknown reason." (Bracket contents have been redacted.)


    Attached is a screenshot. Let me know if you need more of it.


    This certainly sounds like the known issue I mentioned. Calls to resize images were locking the image file, resulting in errors similar to:

    • ColdFusion could not delete the file <path to image file> for an unknown reason

    • An exception occurred while trying to write the image. Ensure that the destination directory exists and that ColdFusion has permission to write to the given path or file. Cause: java.io.FileNotFoundException: /pathToImage/portrait.jpg"

    Important: This particular patch was updated, but the hot fix file name was not changed. So you cannot tell if the proper fix is installed just by looking at file names. AFAIK the only way to tell if you have the correct version installed is to check the modified dates on file itself. (See "notes: at bottom of TechNote)

    http://kb2.adobe.com/cps/403/kb403411.html

    1 reply

    Inspiring
    October 28, 2009

    First thing, see if you have the latest patches and updates applied. There was a well known issue with locked image files that was fixed in one of the CF8 patches

    http://kb2.adobe.com/cps/403/kb403411.html

    Known Participant
    October 28, 2009

    I talked with the guy in charge of our ColdFusion installation and patching, and he claims that it's fully up to date.

    Inspiring
    October 28, 2009

    Can you post the full error message you are receiving?