91网首页-91网页版-91网在线观看-91网站免费观看-91网站永久视频-91网站在线播放

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

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

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

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

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

下面是解決這一問題的幾種方式:

  • __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語句會因此要寫得長一些,但是一個字,值!

<?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)目在采用的一種不錯的方式,就我看來,缺點(diǎn)是,整個項(xiàng)目不方便移動。

例如你一開始放置在xxx.com/,后來需要放到xxx.com/abc/下的話,你要改文件(在一個公有文件中計算ROOT的位置,其他文件包含這個共有文件)。
特別是當(dāng)你同一份代碼放多處時(例如一個測試環(huán)境和一個正式環(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?
這種方式感覺稍嫌麻煩了點(diǎn),隨時要記得恢復(fù)工作目錄也不是容易的事。寫完這句話后,我隨后寫了幾個測試文件,發(fā)現(xiàn)這種方式的最重要缺點(diǎn)不在麻煩,而在它的副作用:改變了工作目錄,這會導(dǎo)致程序邏輯出錯。

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.
這是最方便的方式,但不是沒有缺點(diǎn)。首先,有時候你不見得有權(quán)限修改配置。其次,當(dāng)不同路徑下的文件名有重復(fù)的時候,你會被搞糊涂的(就算你不會,你的維護(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.
如果你每個腳本都需要包含一個通用腳本的話,這幾乎是最好的方式,但是,缺點(diǎn)還是,與配置相關(guān),不夠獨(dú)立。


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

主站蜘蛛池模板: 国产日本三 | 国产太嫩了在线观看 | 国产在线久欧美视频 | 国产精品民宅偷 | 国产福利不 | 中文字幕在线一 | 91国内揄拍国内精 | 92国产精品| 成人性爱视频在 | 国产精品欧美日 | 成人做爰黄级a | 欧美一区二 | 精品第一区二区三区 | 国产猛男gayb | 91香蕉| 国产又粗又猛的视频 | 九九国产福利伦理片 | 精品伊人| 人禽乱交| 日韩丰满 | 91啦丨九色丨刺激 | 国产真实破 | 91秒拍国产福利 | 成人精品一区二区三 | 97小视频| 国产男女插插一级 | 欧美一级特黄 | 国产又粗又长的视频 | 91看片福利 | 午夜剧场| 97午夜理伦片在线 | 日本69sex护士| 日韩精品电影一区 | 日本乱伦欧美综合 | 97国产婷婷综合 | 欧美日韩一级免费 | 91视频完整版高清 | 国产国拍亚洲精品m | 国产精品三三级在线 | 日本在线免费一区 | 午夜神马 |