標籤:http io os 使用 ar strong for 檔案 sp
function initialize()
{
set_include_path(get_include_path().PATH_SEPARATOR . "core/");
set_include_path(get_include_path().PATH_SEPARATOR . "app/");
set_include_path(get_include_path().PATH_SEPARATOR . "admin/");
set_include_path(get_include_path().PATH_SEPARATOR . "lib/");
set_include_path(get_include_path().PATH_SEPARATOR . "include/");
set_include_path(get_include_path().PATH_SEPARATOR."data/");
set_include_path(get_include_path().PATH_SEPARATOR."cache/");
}
這樣它的路徑就成了:
.;C:\php5\pear;core/;app/;admin/;lib/;include/;data/;cache/
哎,我們發現前面還有個.;C:\php5\pear;這到底是怎麼回事呢,其實我們如果什麼也不寫直接先輸出一下include_path的預設值,就會發現它就是.;C:\php5\pear;它可以允許隨便去一個引入檔案。
如果再載入了許多檔案夾的話,我們直接寫檔案名稱就可以了!
但是正如我最開始那個問題一樣,為什麼我們公司的代碼和人家的就不一樣呢,原來,如果我就寫一個
set_include_path(dirname(__FILE__));
然後去引入其他檔案夾的檔案,就會報錯,說在我指定的這個檔案夾內找不到。
首先,我們先用另外一種方法輸出一下:
<?php
set_include_path(dirname(__FILE__));
$include_value = ini_get(‘include_path‘);
echo $include_value;
?>
結果是:D:\AppServ\www
我如果去www下找test4.php這個檔案,則沒有報錯
include("test4.php");
但是我如果去找
include("test1.php");
就會報錯:
Warning: include() [function.include]: Failed opening ‘test1.php‘ for inclusion (include_path=‘D:\AppServ\www‘) in D:\AppServ\www\test.php on line 6
而且我們還發現.;C:\php5\pear;已經被替換掉了。所以我們在使用的時候,如果不是僅在一個檔案夾下引入檔案,我們就需要在前面加上get_include_path().PATH_SEPARATOR .
解釋一下:
get_include_path()是擷取當前include_path的預設值
PATH_SEPARATOR 是個常量,是include的路徑分界符合,在window上是;在unix和Linux上是:
最後,我還要說一下,其實我們也可以通過另外一種方法:即最原始的:
ini_set(‘include_path‘, ‘目錄名‘);
另外,需要注意的兩點就是:
如果在指定的目錄下找不到所要求包含的檔案,而在當前頁面目錄下正好存在這個名稱的檔案時,則預設引入目前的目錄下的該檔案。
當指定了多個目錄為 include_path ,而所要求包含的檔案在這幾個目錄都有相同名稱的檔案存在時,php選擇使用設定 include_path 時排位居前的目錄下的檔案。
set_include_path() &&get_include_path()用法