2012-04-19 52 views
2

嘗試使用循環來檢查圖像是否存在,但它總是返回false。我相信我做一些簡單和愚蠢的,但這裏是代碼:asp fileExists always returns 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 

回答

5

你似乎給人的感覺是當前文件夾背景下,你對FileExists的通話將假設是包含ASP的物理文件夾下操作腳本正在執行。事實並非如此,它很可能是「C:\ windows \ system32 \ inetsrv」。您還正在使用URL路徑元素分隔符/,其中FileExists需要Windows物理路徑文件夾分隔符\

您需要使用Server.MapPath來解析路徑。這可能工作:

if Not fs.FileExists(Server.MapPath("../i/"&sku&".gif")) then 

然而,你可能在與父路徑故障運行「..」,這可能不允許出於安全原因。這可能是一個更好的方法:

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  

其中「parentFolder」是從站點根目錄的絕對路徑。