5

I don't use coldfusion much at all, I'm needed to patch up some code though. Basically I'm trying to check and see if a file I uploaded exist and if it does exist, increment a variable by 1. Then repeat untill I get a unique file name. For whatever reason I can't figure out the proper way to use FileExist(). Some forums suggest using it with len() but those are from 2006 and when I do that it seems to always come out true. Also, when I look at http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c66.html it says it returns either Yes or No. I tried to check against the result various ways, but no luck.

This is the portion of code I have which I am dealing with. The application.filepath is just a variable in my application file which store the expandpath().

<cffile action="upload" destination="#Application.filePath#ListingsGallery/" filefield="iconImage" nameconflict="makeunique">
<cfset iconPlace = #cffile.serverfile#>
<cfset myExt = listLast(iconPlace,".")>
<cfset i = 1 >
<cfset myVar = false>
<cfloop condition="myVar EQ false">

    <cfset newIconName = "iconPhoto" & i &"."& myExt>
    <cfset iconName = Application.filePath & "ListingsGallery/" & #newIconName#>
<cfoutput>#iconName#</cfoutput><br />//just checking to see if it is the correct path, it is.

    <cfif FileExists(iconName) EQ 'Yes'>
         <cfoutput>#myVar#</cfoutput> //checking the value, it never hits here.
    <cfelse>
             <cfoutput>#myVar#</cfoutput><br /> //checking the value, it always hits here.
    <cfset myVar = true>        
             <cfoutput>#myVar#</cfoutput> //Again check the value.
    </cfif>
<cfset i++>
</cfloop>                     
<cffile action="rename" source="#Application.filePath#ListingsGallery/#iconPlace#" destination="#Application.filePath#ListingsGallery/#newIconName#">

The absolute path on a unix server is something like, /var/www/website folder name/ etc.... Correct? That's the absolute server path, the coldfusion docs seem to specify at least a microsoft absolute server path so I'm assuming this is what is needed.

Edit--------------------------- PS: I can' only give one of you credit, so I gave it to Kruger since he came a minute earlier. lol...

d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • What does iconName look like? I see you are outputting it to test, which is good. As for paths, I recommend using /. It works in Windows + Linux/OSX systems. – Raymond Camden Apr 30 '12 at 19:38
  • `` is a string comparison. You should probably just do `` since FileExists returns a boolean value. – ale Apr 30 '12 at 19:59

4 Answers4

5

FileExists() returns a boolean value. This should work fine now that the typo has been fixed:

<cfif fileExists(TheFile)>
  // do this
<cfelse>
  // do that
</cfif>
Community
  • 1
  • 1
Evik James
  • 10,335
  • 18
  • 71
  • 122
  • 1
    That worked, for the record though the coldfusion docs say it returns either a 'Yes' or 'No' no mention of a boolean value or true/false state. http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c66.html I understand Yes/no on/off but I've never seen it stated that way so I figured they meant the literal 'Yes' or 'No'. Thank you all for you help. – d.lanza38 Apr 30 '12 at 20:37
  • There are several places in CF code where only true/false work. It's just a little buggy and there is some errors in documentation. I am glad I could be of some help. – Evik James Apr 30 '12 at 21:07
  • @Evik - There may be a few special cases (I cannot recall offhand), but I am pretty sure this is not one of them :) Either syntax should work. It is just a matter of style. Though I prefer yours. – Leigh Apr 30 '12 at 21:18
  • @Leigh, can you test to see if this works with yes/no/true/false. I believe that is does NOT work in CF8 with yes/no – Evik James Apr 30 '12 at 21:47
  • @Evik - I tried it with CF8 and 9, and it worked with both. I think the two would have to be equivalent for backward compatibility with legacy code. – Leigh Apr 30 '12 at 22:29
  • The YES/NO nonsense is so annoying! - If they really must use strings for their booleans, at least use the words true/false. `` – Mike Causer May 03 '12 at 06:49
  • 1
    @Mike - Oh the things we do for backward compatibility ;) – Leigh May 03 '12 at 22:21
  • 1
    Typo in that, it's exists not exist. – Patrick Schomburg Sep 14 '17 at 13:49
3

Assuming your application.Filepath is the correct file path you are on the right track. It looks like your upload directory might be beneath the web root - considering moving it outside the web root for security. Take a look at #expandPath('.')# as a way of creating guaranteed file paths without typos :) Also makes your code more portable.

To my eye the code above would work. FYI - you don't need "EQ 'YES'. You are fine to just do:

<Cfif FileExists(iconName)>...

You could also do

condition="NOT myVar">

There are several ways to handle logic code in CF.

If your fileExists() never hits take a closer look at your rename. Are you throwing an eror?

Mark A Kruger
  • 7,183
  • 20
  • 21
  • Hey.... just a note - your code is somewhat superfluous .. the "makeunique" attribute on your file upload will guarantee uniqueness... – Mark A Kruger Apr 30 '12 at 20:15
  • Yes, but the issues I'm having that caused me to undertake this have to do with people uploading images with spaces. They turn into %20 in the database but remain a space on the server. So the images never appear and can't be deleted based on the database entry either. I supposed I could do some sort of str_replace but if I can snip it here I'd rather not have to go through and modify every reference to these images. – d.lanza38 Apr 30 '12 at 20:30
  • Then I'd say you are on the right track... I've written some code like this myself a few times :) – Mark A Kruger Apr 30 '12 at 20:54
3

I can't add notes to answers yet, but I wanted to let the OP know that CF is typeless when it comes to boolean evaluations in functions. 0 is the same as "no" is the same as "false", whereas any positive number is the same as "yes" is the same as "true".

Sharondio
  • 2,605
  • 13
  • 16
-1

Its great to have back-end security, but this should have been handled on the front-end so you never get values you don't want. The new HTML5 input patterns would prevent this from ever being submitted, so you wouldn't have to fix this on the back end.

http://www.w3schools.com/tags/att_input_pattern.asp

crisprex
  • 11
  • 2
  • 1
    I think you misunderstood the question. They were asking about renaming *server side* files. Not something that can be done with client side code. – Leigh Jun 23 '16 at 17:37
  • html5 input patterns should not be used for security. There are other ways of throwing data at a server that bypass the browser. – Tad M. Aug 22 '16 at 15:18