2013-03-23 353 views
0

使用file_exists(時),它總是返回false。但該文件實際存在,可以使用require加載。這是我的代碼:爲什麼file_exists總是返回false?

// Creating dependency lists. 
$data_dependency = array("mysql", "oracle", "postgresql", "sqlite", "access"); 

// Calling the dependecny files. 
foreach ($data_dependency as $list) { 
    $list = "class.data.$list.php"; 
    _system::debug("Calling required class $list ..."); 

    if (file_exists($list)) { 
     include($list); 
     _system::debug("Calling $list was successfull."); 
    } else { 
     _system::debug("Calling $list was failed. Now we close the system load."); 
     exit("Calling $list was failed. Now we close the system load."); 
    } 
} 

當我加載頁面時,頁面總是exit()。我認爲原因是file_exists()函數總是返回false。

+1

嘗試使用絕對路徑。 – C9HDN 2013-03-23 14:09:00

+6

你說文件單數存在,但你正在尋找5,如果任何一個不存在它將退出 – Crisp 2013-03-23 14:09:05

+0

@Calum - 我試圖使用絕對路徑,但仍然是錯誤的。 – 2013-03-23 14:25:58

回答

-1

$list在您的foreach循環中可能設置不正確。你有沒有試過$list="class.data.".$list.".php";

+3

所以你知道,PHP用雙引號評估變量。所以他的'$ list'的構建是正確的 – UnholyRanger 2013-03-23 14:23:40

+0

'$ list'是正確的。當我回顯'$ list'時,輸出將會是「class.data.mysql.php」。 – 2013-03-23 14:31:25

0

關於file_exists() Php.net指出

該功能因爲安全模式的限制將返回FALSE爲不可訪問的文件。但是,如果這些文件位於safe_mode_include_dir中,它們仍然可以包含在內。

這可以解釋包含工作和存在檢查沒有的事實。

http://www.php.net/manual/en/function.file-exists.php

+1

我認爲'safe_mode'已從PHP 5.4.0中移除。我使用PHP 5.4.7。 – 2013-03-23 14:35:01

2

該函數返回FALSE對由於安全模式的限制無法訪問的文件。

在文件路徑中使用$_SERVER['DOCUMENT_ROOT']變量。

1

嘗試使用相對路徑ex : $_SERVER['DOCUMENT_ROOT']而不是絕對路徑

相關問題