php中定義了一個很有用的常數,即:__file__
這個内定常數是所在php程式檔案的完整路徑(路徑+檔案名)。
即使這個檔案被其他檔案引用(include或require),__file__始終是它所在檔案的完整路徑,而不是引用它的那個檔案完整路徑。
請看下面例子:
D:\phpStudy\upload\test\a.php
<?php
$the_full_name=__FILE__;
$the_dir=dirname(__FILE__);
echo $the_full_name; //傳回:D:\phpStudy\upload\test\a.php
echo $the_dir; //傳回:D:\phpStudy\upload\test
?>
D:\phpStudy\upload\b.php
<?php
include "test/a.php";
echo $the_full_name; //傳回:D:\phpStudy\upload\test\a.php
echo $the_dir; //傳回:D:\phpStudy\upload\test
?>
簡單地說:
__FILE__ 傳回其所在檔案的路徑+檔案名
dirname() 該函數傳回去掉檔案名後的目錄名(路徑)。
dirname(__FILE__) 傳回__FILE__所在檔案的路徑部分
dirname(dirname(__FILE__));得到的是__FILE__所在檔案的上一層目錄名(不含最後一個“\”号)
例如,目前檔案是 D:\phpStudy\upload\test\a.php,則
__FILE__ 得到的就是完整路徑 即 D:\phpStudy\upload\test\a.php ,而
dirname(__FILE__)得到路徑部分 即 D:\phpStudy\upload\test
dirname(dirname(__FILE__)),即:D:\phpStudy\upload