2010-02-25 71 views
2

在我的腳本中,我設置了包含路徑(因此應用程序的另一部分也可以包含文件),檢查文件是否存在幷包含它。PHP:可以包含file_exists()表示不存在的文件

但是,在設置包含路徑後,file_exists()報告文件不存在,但我仍然可以包含相同的文件。

<?php 
    $include_path = realpath('path/to/some/directory'); 
    if(!is_string($include_path) || !is_dir($include_path)) 
    { 
    return false; 
    } 
    set_include_path(
    implode(PATH_SEPARATOR, array(
     $include_path, 
     get_include_path() 
    )) 
); 
    // Bootstrap file is located at: "path/to/some/directory/bootstrap.php". 
    $bootstrap = 'bootstrap.php'; 

    // Returns "bool(true)". 
    var_dump(file_exists($include_path . '/' . $bootstrap)); 
    // Returns "bool(false)". 
    var_dump(file_exists($bootstrap)); 

    // This led me to believe that the include path was not being set properly. 
    // But it is. The next thing is what puzzles me. 

    require_once $bootstrap; 
    // Not only are there no errors, but the file is included successfully! 

我可以在不提供絕對文件路徑的情況下編輯包含路徑和包含文件,但我無法檢查它們是否存在。這實際上很煩人,因爲每次調用不存在的文件時,我的應用程序都會導致嚴重錯誤,或者最多隻會發出警告(使用include_once())。

不幸的是,關閉錯誤和警告不是一種選擇。

任何人都可以解釋是什麼造成這種行爲?

回答

3

file_exists無非就是說文件是否存在(並允許腳本知道它存在),解析相對於cwd的路徑。它並不關心包含路徑。

+0

感謝,最簡單的方式,我認爲這是最有可能的情況下,也沒有找到PHP手冊中的任何提及。感謝澄清:) – zanbaldwin 2010-02-25 02:04:18

0

是這裏是實現這個

$file_name = //Pass File name 
if (file_exists($file_name)) 
      { 
       echo "Exist"; 
      } 
     else 
      { 
       echo "Not Exist"; 
      }