2012-02-25 25 views
4

這裏是我的代碼:需要()失敗,即使file_exists()債權文件是存在的

if (file_exists('config.php')) { 
    require('config.php'); // This is line 38 
} 

這在某種程度上產生錯誤:

Warning: require(config.php) [function.require]: failed to open stream: No such file or directory in /path/name/file.php on line 38

如何在地球上這可能嗎?

更新:下面確實工作:

if (file_exists(getcwd(). '/config.php')) { 
    require(getcwd(). '/config.php'); 
} 
+0

奇怪。你可以在這些行之前嘗試'clearstatcache()'嗎? – 2012-02-25 22:19:56

+0

似乎沒有影響任何東西。看到我更新的問題。 – Mike 2012-02-25 22:24:28

+2

'is_readable()'而不是?在這種情況下,file_exists並不是特別有用,因爲您需要該文件實際上可被腳本讀取。把它看作是「銀行金庫裏有錢嗎」的區別?和「我可以把一些錢存入保險庫嗎?」 – 2012-02-25 22:24:46

回答

4

試圖忽略包含路徑:

if (file_exists('config.php')) { 
    require('./config.php'); // This is line 38 
} 

,如果它的工作原理你缺少。目錄到包含路徑,你必須選擇將其納入或使用相對路徑文件名

您可以用PHP的配置指令改變你的include_path(如果你可以改變PHP配置文件),或訴諸get_include_path()在每個文件/項目基地通過set_include_path()

例如:set_include_path('.'. PATH_SEPARATOR . get_include_path());在你的PHP的第一行(或在共同的配置文件);

來源:

+0

謝謝你和Ed Heal的解釋。這就是發生了什麼事。 – Mike 2012-02-25 22:40:41

4

的PHP includerequire操作以同樣的方式,並使用包括路徑(include)。因此,將使用此路徑來查找該文件,如果您沒有正確設置它,它將不會查看當前目錄。

使用get include path找出這個值。

5

嘗試絕對路徑。使用dirname(__FILE__)

require(dirname(__FILE__) . '/config.php'); 
相關問題