asp fileExists总是返回false

kql*_*ert 2 asp-classic

尝试使用循环来检查图像是否存在但是它总是返回false.我确信我正在做一些简单而愚蠢的事情,但这里是代码:

dim fs, sql_except
set fs=Server.CreateObject("Scripting.FileSystemObject")
if Not rs.eof then
    arrRS = rs.GetRows(30,0)
    set rs = nothing
    If IsArray(arrRS) Then
        For i = LBound(arrRS, 2) to UBound(arrRS, 2)
            sku = arrRS(0, i)
            if (fs.FileExists("../i/"&sku&".gif")=false) Then
                response.write sku&"does not exist<br>"
            end if
        next
    end if
    erase arrRS
end if
set fs=nothing
Run Code Online (Sandbox Code Playgroud)

Ant*_*nes 5

您似乎正在操作的印象是您调用的当前文件夹上下文FileExists将假定是包含正在执行的ASP脚本的物理文件夹.事实并非如此,它很可能是"C:\ windows\system32\inetsrv".您还使用URL路径元素分隔符/,其中FileExists期望Windows物理路径文件夹分隔符\.

您需要使用Server.MapPath来解决路径.这可能有效:

if Not fs.FileExists(Server.MapPath("../i/"&sku&".gif")) then
Run Code Online (Sandbox Code Playgroud)

但是,您可能会遇到父路径".."的问题,出于安全原因,可能不允许这样做.这可能是一种更好的方法:

Dim path : path = Server.MapPath("/parentFolder/i") & "\"
For i = LBound(arrRS, 2) to UBound(arrRS, 2)        
    sku = arrRS(0, i)        
    if Not fs.FileExists(path & sku & ".gif") Then        
        response.write Server.HTMLEncode(sku) & " does not exist<br>"        
    end if        
next    
Run Code Online (Sandbox Code Playgroud)

其中"parentFolder"是站点根目录的绝对路径.