超碰人人人人人,亚洲AV午夜福利精品一区二区,亚洲欧美综合区丁香五月1区,日韩欧美亚洲系列

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
版主

PHP中include路徑的解決方法匯總

machangmu
2012年8月6日 9:23 本文熱度 3996

這幾天整理一份很亂的代碼,這才意識(shí)到php對(duì)include處理不是一般的賤:別的編程語(yǔ)言在處理include中的相對(duì)目錄時(shí),都是以當(dāng)前處理的文件作為基準(zhǔn)。也就是說(shuō),如果A包含B,B包含C時(shí),C再包含一個(gè)含相對(duì)路徑的文件,那么路徑是相對(duì)于C的。這樣的處理很自然,符合人們的直覺(jué),也便于開(kāi)發(fā)出路徑無(wú)關(guān)的程序包。

可是PHP不這樣,它優(yōu)先相對(duì)工作目錄來(lái)處理,并且如果路徑中包含. ..的話,則只相對(duì)于工作目錄。
也許PHP這樣處理有它的理由,有誰(shuí)知道的不妨告訴我。

下面是解決這一問(wèn)題的幾種方式:

  • __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語(yǔ)句會(huì)因此要寫(xiě)得長(zhǎng)一些,但是一個(gè)字,值!

<?php 

include dirname(__FILE__).'/subdir';

//dirname return value does not contain the trailing slash

?>

  • $_SERVER['DOCUMENT_ROOT']

This method allows you to specify a path relative to the web server doc_root for file inclusion.
這也是許多項(xiàng)目在采用的一種不錯(cuò)的方式,就我看來(lái),缺點(diǎn)是,整個(gè)項(xiàng)目不方便移動(dòng)。

例如你一開(kāi)始放置在xxx.com/,后來(lái)需要放到xxx.com/abc/下的話,你要改文件(在一個(gè)公有文件中計(jì)算ROOT的位置,其他文件包含這個(gè)共有文件)。
特別是當(dāng)你同一份代碼放多處時(shí)(例如一個(gè)測(cè)試環(huán)境和一個(gè)正式環(huán)境),你改文件也不好改。

<?php

if (!defined("WETSITE_BASE_DIR"))

define("WETSITE_BASE_DIR", $_SERVER['DOCUMENT_ROOT'].'/Clare/');

require_once(WETSITE_BASE_DIR.'includes/global.inc.php');

?>

  • 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?
這種方式感覺(jué)稍嫌麻煩了點(diǎn),隨時(shí)要記得恢復(fù)工作目錄也不是容易的事。寫(xiě)完這句話后,我隨后寫(xiě)了幾個(gè)測(cè)試文件,發(fā)現(xiàn)這種方式的最重要缺點(diǎn)不在麻煩,而在它的副作用:改變了工作目錄,這會(huì)導(dǎo)致程序邏輯出錯(cuò)。

rainfalling at yahoo dot com (21-Sep-2005 01:06)

This is yet another way to include files relative to the current file. I find it easier if you have a lot of includes.

<?php

$prewd = getcwd(); // get the current working directory

chdir(realpath(dirname(__FILE__))); // change working directory to the location of this file

include('includedfile.php'); // include relative to this file

chdir($prewd); // change back to previous working dir

?>

  • set_include_path()

This way is the most convenient way but it's not without flaws. First, not in all cases you have permission to change server configuration. Second, if there are many path specified in include_path, the actually included file may not be the one you expected because there may be files of the same name under different directories.
這是最方便的方式,但不是沒(méi)有缺點(diǎn)。首先,有時(shí)候你不見(jiàn)得有權(quán)限修改配置。其次,當(dāng)不同路徑下的文件名有重復(fù)的時(shí)候,你會(huì)被搞糊涂的(就算你不會(huì),你的維護(hù)者呢)。

  • auto_prepend_file and auto_append_file in php.ini

This almost the best way if your scripts commonly need a startup script. We can do a lot of useful things in the startup script, for examples, define constants, load configurations. But it's not always OK to change the php.ini settings. Remember the most adaptive application should be as independent from configs as possible.
如果你每個(gè)腳本都需要包含一個(gè)通用腳本的話,這幾乎是最好的方式,但是,缺點(diǎn)還是,與配置相關(guān),不夠獨(dú)立。


該文章在 2012/8/6 9:29:26 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved