標籤:des style blog color io ar for sp 檔案
1.__FILE__
__FILE__ always equals to the real path of a php script regardless whether it‘s included.
__FILE__ helps you specify the file to include using relative path to the including file.
這種方法首選推薦。雖然你的include語句會因此要寫得長一些,但是一個字,值!
<?phpinclude dirname(__FILE__).‘/subdir‘;//dirname return value does not contain the trailing slash?>
2.$_SERVER[‘DOCUMENT_ROOT‘]
This method allows you to specify a path relative to the web server doc_root for file inclusion.
這也是許多項目在採用的一種不錯的方式。
<?phpif (!defined("WETSITE_BASE_DIR"))define("WETSITE_BASE_DIR", $_SERVER[‘DOCUMENT_ROOT‘].‘/Clare/‘);require_once(WETSITE_BASE_DIR.‘includes/global.inc.php‘);?>
3.chdir()
The include looks for file relative to current working directory. We can use this feature. It‘s really a "fancy" way, but I‘m not sure whether it‘s safe all the time. Who knows?
這種方式感覺稍嫌麻煩了點,隨時要記得恢複工作目錄也不是容易的事。寫完這句話後,我隨後寫了幾個測試檔案,發現這種方式的最重要缺點不在麻煩,而在它的副作用:改變了工作目錄,這會導致程式邏輯出錯。
<?php$prewd = getcwd(); // get the current working directorychdir(realpath(dirname(__FILE__))); // change working directory to the location of this file include(‘includedfile.php‘); // include relative to this filechdir($prewd); // change back to previous working dir?>
4.set_include_path()
這是最方便的方式,但不是沒有缺點。首先,有時候你不見得有許可權修改配置。其次,當不同路徑下的檔案名稱有重複的時候,你會被搞糊塗的(就算你不會,你的維護者呢)。
5.auto_prepend_file and auto_append_file in php.ini
如果你每個指令碼都需要包含一個泛型指令碼的話,這幾乎是最好的方式,但是,缺點還是,與配置相關,不夠獨立。
PHP中include路徑修改