PHP中include路徑修改

來源:互聯網
上載者:User

標籤: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路徑修改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.