如何正确使用Coldfusion的FileExist()方法?

d.l*_*a38 5 coldfusion file-exists

我根本不使用coldfusion,但我需要修补一些代码.基本上我正在尝试检查并查看我上传的文件是否存在以及是否存在,将变量增加1.然后重复直到我得到一个唯一的文件名.无论出于何种原因,我无法弄清楚使用FileExist()的正确方法.一些论坛建议将它与len()一起使用,但这些是从2006年开始的,当我这样做时,它似乎总是真实的.此外,当我查看http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c66.html它说它返回是或否.我试图以各种方式检查结果,但没有运气.

这是我正在处理的代码部分.application.filepath只是我的应用程序文件中的一个变量,它存储了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#">
Run Code Online (Sandbox Code Playgroud)

unix服务器上的绝对路径类似于/ var/www/website文件夹名称/ etc ....正确吗?这是绝对的服务器路径,coldfusion文档似乎至少指定了一个微软绝对服务器路径,所以我假设这是需要的.

编辑--------------------------- PS:我只能给你们其中一个人,所以我把它给了Kruger,因为他来了早一点.大声笑...

Evi*_*mes 5

FileExists()返回一个布尔值.这个问题应该可以解决,因为错字已经修复:

<cfif fileExists(TheFile)>
  // do this
<cfelse>
  // do that
</cfif>
Run Code Online (Sandbox Code Playgroud)


Mar*_*ger 3

假设您的 application.Filepath 是正确的文件路径,您就在正确的轨道上。您的上传目录可能位于 Web 根目录下 -为了安全考虑将其移至 Web 根目录之外。看一下 #expandPath('.')# 作为创建有保证的文件路径而不会出现拼写错误的方法:) 也使您的代码更加可移植。

在我看来,上面的代码可以工作。仅供参考 - 你不需要“EQ 'YES'。你可以这样做:

<Cfif FileExists(iconName)>...
Run Code Online (Sandbox Code Playgroud)

你也可以这样做

condition="NOT myVar">
Run Code Online (Sandbox Code Playgroud)

CF 中有多种处理逻辑代码的方法。

如果您的 fileExists() 从未命中,请仔细查看您的重命名。你抛出一个错误吗?