fileExist() does not recognize any files I provide it

617 Views Asked by At

I have a file upload after a form submission and I need to delete the old file that was there after my file upload is complete. Everything works properly, form submission, file upload. But Coldfusion in general doesn't seem to be able to recognize the old file. Either with a fileExist() command or in the actually <cffile action="delete"> command. Mean while I am output the variable I am using for both fileExist() and the delete right before I hit those lines and I navigate to that exact path in my ftp and the file is in fact there, before and after form submission. The files upload should not be overwriting the previous photo as they are named completely differently and makeunique is being used. I would be grateful for any help.

Here is the snippet of code I have for after form submission (I'm only trying to get image1 working at the moment, but the other 3 images are display the same behavior.

This line executes <cfoutput>#variables.erase1#</cfoutput><br /> but hello2 doesn't:

<cfquery name="getPreviousImage" datasource="#Application.datasourceName#">
        SELECT 
            image1, image2, image3, image4
        FROM 
            testaCustomers
        WHERE 
            RecordID = <cfqueryparam value="#form.ID#" cfsqltype="CF_SQL_INTEGER" maxlength="50">

</cfquery> 

<cfset variables.oldImage1 = getPreviousImage.image1>
<cfset variables.oldImage2 = getPreviousImage.image2>
<cfset variables.oldImage3 = getPreviousImage.image3>
<cfset variables.oldImage4 = getPreviousImage.image4>

<cfset variables.image1 = getPreviousImage.image1>
<cfset variables.image2 = getPreviousImage.image2>
<cfset variables.image3 = getPreviousImage.image3>
<cfset variables.image4 = getPreviousImage.image4>

<cfif #form.image1# NEQ ""> 

    <cffile action="upload" destination="#Application.filePath#Pics/" filefield="image1" nameconflict="makeunique" result="upload1">

    <cfif isDefined ("upload1.serverFile")>
        <cfset variables.image1 = #upload1.serverFile#> 
    </cfif>

   <cfset variables.erase1 = Application.filePath & "Pics/" & variables.oldImage1>
   <cfoutput>#variables.erase1#</cfoutput><br />

    <cfif fileExists(variables.erase1)>
        hello2
        <cffile action="delete" file="#variables.erase1#">
    </cfif>
</cfif>
2

There are 2 best solutions below

0
On

You may have to wrap your image path in an ExpandPath() function like you did with Trim();

<cfif fileExists(ExpandPath(Trim(variables.erase1)))>
    ...code...
</cfif>

FileExists() expects a physical path not a web root path. ExpandPath() makes the conversion from a web site path to a physical path (*nix & windows).

4
On

Check your file permissions. The account running your application service may not have read access to the files you're trying to access. Otherwise, your path may be incorrect.