0

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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
d.lanza38
  • 2,525
  • 7
  • 30
  • 52

2 Answers2

1

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.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Josh Siok
  • 936
  • 4
  • 8
  • I've changed the permissions to 777. It still doesn't read it as a file. Plus the uploads work fine. – d.lanza38 May 15 '12 at 20:38
  • As a troubleshooting step, you could use cfdirectory to get the contents of #application.filePath#Pics/ . cfdump the results and see if ColdFusion can see those files. – Josh Siok May 15 '12 at 20:58
  • I got it, trim(). My entries were getting inserted into the database with spaces added to the end. I guess html trims src="" by default and coldfusion doesn't, because I saw the images being displayed in the browser. – d.lanza38 May 15 '12 at 21:12
  • Since it was a path problem, feel free to mark Josh's response as the answer :) – Leigh May 16 '12 at 01:42
0

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).

Scott Jibben
  • 2,229
  • 1
  • 14
  • 22