2013-01-05 57 views
1

我很难过,我确信我错过了一些非常基本的东西。安全模式被禁用,文件存在。当文件名通过变量传递时,file_exists返回false

此代码:

$dir = "textfiles" 
chdir('../'.$dir."/"); 
    //using a Windows Slash here(Wamp Stack, on Windows 7 Dev environment 

$filename = getcwd() ."\\". $row[0]; 

     //echoing this outputs: 
     //C:\wamp\www\wordpress\textfiles\New Text Document.txt 


$filename = str_replace("\\","\\\\",$filename); 

     //echoing this outputs C:\\wamp\\www\\wordpress\\textfiles\\New Text Document.txt 
     //escaping slashes in filename to prevent escaping. I SUSPECT my issue may be 
     // related to this 

    //if (file_exists("C:\\wamp\\www\\wordpress\\textfiles\\New Text Document.txt")) { 
      //Line above is commented out, but when it replaces the line below, this thing      
      //returns True 


if (file_exists($filename)) {echo "Yes";} 
    else { echo $filename;} 
+0

第一行中是否缺少分号? – inhan

+0

此外,您可能需要使用目录常量[**'DIRECTORY_SEPARATOR' **](http://php.net/manual/en/dir.constants.php),而不是明确写入分隔符。 – inhan

回答

0

注释掉线

/* $filename = str_replace("\\","\\\\",$filename); */ 

您的代码应工作。

+0

不幸的是,没有。我试了很多次,然后又做了。感谢您的回应。 –

+0

更正:您是对的,因为我的数据是从数据库中提取的,所以在文件名末尾有一个错误的空间。一个简单的小修剪()解决了我的问题 –

+0

然后,你需要弄清楚为什么有这样的空间;-) – Aivar