php中好像不能像asp那样用“/”表示根目录,代之以$_SERVER['DOCUMENT_ROOT'],其它则相同:../表示向上一层。./表示当前层。假如现在a/b/c/s.php要调用根目录下的 /bb/s2.txt,则:
$RootDir = $_SERVER['DOCUMENT_ROOT']; $fireDir = "$RootDir/bb/s2.txt"; |
或者:“../../../bb/s2.txt”表示向上返回到b再向上到a再向上到根目录然后到bb下。
前一阵子老是受php开发中,文件互相引入require()相对位置关系的困扰,为了彻底弄清它们的关系,笔者做了个实验。
以下是实验图:
当前项目(project2)的绝对路径是:D:\www\php_case\Coucom_make。也就是我们当前项目的根目录root.
为了能更加清楚的表述不同级别的目录文件相互的引入问题,偶大胆将引用分成三种类型即:上级对下级的引用(简称上级引用,英文译为:superior to underling。简称(stou)).
下级对上级的引用(简称下级引用,英文反之便是)
平级引用或叫同级引用(英文:paratactic)。
好了,我们明确了引用类型,下面我们来看不同类型引用它们有着什么样的规则。
我们先来说说上级引用:
看我们的实验图,在图中项目下分别有aa bb ee 三个同级目录和一个index.php文件,在bb下又有cc目录,cc下又有dd目录和cc.php ccc.php两个文件,同样dd下也包含一个dd.php文件。凡是上层对下层均属上级引用.
例如:index.php对于所有文件的引用: cc .php对dd.php的引用: ee.php对dd.php的引用: |
大家仔细看一下目录结构,便会发现这三种引用虽然同属于上级引用,但它们又不完全相同,我把它分成两种情况:即在上级引用中存在两种引用情况:1.从属的上级引用(类如cc.php对dd.php,,因为这两个文件同属于cc目录)2.非从属的上级引用(类如index.php对于所有文件的引用和ee.php对dd.php的引用都属于这种情况因为它们与被引用的文件并没有一个共同的父目录,在能相对于站点根目录).
对于从属上级引用:
以下是在cc.php对dd.php的引用
require('dd/dd.php'); |
对于非从属上级引用:
以下是在ee.php中对cc.php的引用
require('../bb/cc/cc.php'); |
The above mentioned are upper-level quotes, let’s learn about lower-level quotes below! In the same way, references from lower layers to upper layers are subordinate references, which are also divided into subordinate and non-subordinate categories. The relative paths of subordinate subordinate references take their parent directory as the root directory, for example:
Ttt.php’s reference to bbff.php falls into this situation: require('../bb/cc/cc.php');
Non-affiliated subordinate references are based on the root directory of the website, for example:
ccc.php’s reference to ee.php: require('../../ee/ee.php');
The above is an introduction to subordinate references. Finally, let’s take a look at parallel references, or peer references. In fact, peer references are also divided into two situations: subordinate peer references and non-subordinate peer references
Dependent sibling references are simple: references to two files in the same directory
For example: refer to ttt.php in dd.php require('ttt.php');
Non-subordinate peer reference: that is, a reference to two files that are not in the same directory (no common parent directory, only the website directory is the parent directory), but have the same level, for example: in aa. php is referencing ee.php require('../ee/ee.php');
The above are the references in three types of situations, and there are also issues involving nested references
For example:
ff.php refers to dd.php, and dd.php refers to gf.php. In this case, originally dd.php referencing gf.php is a non-subordinate reference in the subordinate reference. The writing method is like this: require( '../../../ee/gf.php'); and ff.php's reference to dd.php is a subordinate reference in the superior reference. It is written like this: require('./cc/dd/dd. php'); But you will find that the gf.php file cannot be found in ff.php, so how to write it? I tell you that you should write this in dd.php: require('../ee/gf.php'); This is the only correct way to write it. Why? Because when it comes to nested references, the relative path of the referenced file must be based on the final referenced file!
In short:
In fact, it’s very simple. You use your web root directory as the root directory. No matter how you decide, your files must have a file that must be included in all files. For example, some are global.php, and some are common. php
If this file is placed in the root directory
wwwroot/global.php |
In the first line, add chdir(dirname(__FILE__)); //Switch to the directory where global.php is located and follow the directory
Use other files
require "../../../global.php"; require "aa/aa.php"; require "bb/bb/cc.php"; |
That’s it, because your global.php has already switched the path to wwwroot, so you don’t have to go through as much trouble as many people
define('ROOT_PATH',dirname(__FILE__)); require ROOT_PATH..... |
chdir(dirname(__FILE__)); is indeed very easy to use. When importing this file at a relative position, other imported files only need to be based on it.