【问题标题】:fileExist() does not recognize any files I provide itfileExist() 无法识别我提供的任何文件
【发布时间】:2012-05-23 09:45:20
【问题描述】:

我在提交表单后上传了文件,我需要删除文件上传完成后的旧文件。一切正常,表单提交,文件上传。但一般来说 Coldfusion 似乎无法识别旧文件。使用fileExist() 命令或实际的<cffile action="delete"> 命令。同时,当我输出用于fileExist() 和删除的变量时,就在我点击这些行之前,我导航到我的 ftp 中的那个确切路径,并且在表单提交之前和之后,文件实际上就在那里。上传的文件不应覆盖之前的照片,因为它们的名称完全不同,并且正在使用makeunique。如果有任何帮助,我将不胜感激。

这是我提交表单后的代码 sn-p(我现在只是想让 image1 工作,但其他 3 个图像显示相同的行为。

此行执行<cfoutput>#variables.erase1#</cfoutput><br />hello2 不执行:

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

【问题讨论】:

  • #variables.erase1# 输出什么?绝对文件路径?您可以将其添加到问题中吗?
  • 是的,它是绝对文件路径。这就是它输出的内容“/var/www/testaproduce.com/Pics/Winter8.jpg”

标签: coldfusion file-exists cffile


【解决方案1】:

检查您的文件权限。运行您的应用程序服务的帐户可能对您尝试访问的文件没有读取权限。否则,您的路径可能不正确。

【讨论】:

  • 我已将权限更改为 777。它仍然不会将其作为文件读取。加上上传工作正常。
  • 作为故障排除步骤,您可以使用 cfdirectory 获取 #application.filePath#Pics/ 的内容。 cfdump 结果并查看 ColdFusion 是否可以看到这些文件。
  • 我明白了,trim()。我的条目被插入到数据库中,并在末尾添加了空格。我猜 html 默认会修剪 src="" 而coldfusion不会,因为我看到了浏览器中显示的图像。
  • 由于是路径问题,请随意将 Josh 的回复标记为答案 :)
【解决方案2】:

您可能必须像使用 Trim() 一样将图像路径包装在 ExpandPath() 函数中;

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

FileExists() 需要物理路径而不是 Web 根路径。 ExpandPath() 将网站路径转换为物理路径(*nix & windows)。

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2016-07-03
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多