An analysis of the relationship between absolute and relative paths in PHP

  • 2020-03-31 20:23:11
  • OfStack

PHP does not seem to be able to use "/" to represent the root directory as asp does, instead of $_SERVER['DOCUMENT_ROOT'], the rest is the same:.. / means one level up. ./ represents the current layer. If a/b/c/ s.hp now calls /bb/s2.txt in the root directory, then:

$RootDir = $_SERVER [' DOCUMENT_ROOT];
$fireDir = "$RootDir/bb/s2. TXT".

Or: ".. /.. /.. /bb/s2. TXT "means go up to b and up to a and up to the root and then down to bb.

To get to the bottom of the problem with PHP, where files introduce require() relative to each other, I did an experiment.

The following is the experiment diagram:

< img Alt = "" border = 0 height = 286 SRC =" http://files.jb51.net/upload/2010-3/20100303132424572.jpg "width = 148 border = 0 >

The absolute path for the current project (project2) is: D:\ WWW \php_case\Coucom_make. This is the root of our current project.

In order to make it clear that different levels of directory files introduce each other, I boldly divided the reference into three types: superior to subordinate reference. Short for (stou).

A reference from a subordinate to a superior

A horizontal or peer quotation (paratactic).

Ok, now that we've defined the reference types, let's see what the rules are for different types of references.

Let's start with the superior quote:

According to our experiment diagram, under the project in the figure, there are three directories with the same level as aa, bb, ee and an index.php file respectively. Under bb, there are cc directory, under cc, dd directory and cc.php cc.php files. Similarly, under dd, there is also a dd.php file. Always the upper to the lower are superior references.

For example: index.php references to all files:
Cc.php reference to dd.php:
Ee.php reference to dd.php:

If you take a closer look at the directory structure, you will see that these three references, although they belong to the parent reference, are not identical. Dependent parent references (such as cc.php versus dd.php, because both files belong to the cc directory) 2. Non-dependent parent references (such as index.php references to all files and ee.php references to dd.php are in this case because they do not share a parent directory with the referenced file, relative to the site root).

For subordinate parent references:

The following is a reference to dd.php in cc.php

Require (' dd/dd. PHP);

For non-subordinate parent references:

The following is a reference to cc.php in ee.php

Require (".. / bb/cc/cc. PHP ');

What said above is the superior quote, below we will understand the subordinate quote! Similarly, references from the lower level to the upper level belong to subordinate references, which are also divided into two categories: dependent and non-dependent. Relative paths of subordinate references take their parent directory as the root directory, for example:

Ttt.php's reference to bbff.php falls into this category: require('.. / bb/cc/cc. PHP ');

Non-subordinate subordinate references are based on the root of the site, for example:

Ccc. PHP reference to ee.php: require('.. /.. / ee/ee. PHP ');

That's the introduction of subordinate references, and finally we'll look at the level references or the level references, which are also divided into two types: dependent level references and non-dependent level references

Dependent sibling references are simple: references to two files in the same directory

For example, refer to ttt.php require('ttt.php') in dd.php;

Non-dependent sibling references: that is, references to two files that are not in the same directory (there is no common parent directory, only if the destination is the parent directory), but the level is the same, e.g.   Require (".. / ee/ee. PHP ');

These are three different types of references, and there is also the issue of nested references

Such as:

Ff.php refers to dd.php, and dd.php refers to gf.php. In this case, dd.php refers to gf.php, which belongs to a non-dependent reference in a subordinate reference. /.. /.. / ee/gf, PHP '); The ff.php reference dd.php belongs to a subordinate reference in a parent reference, written like this: require('./cc/dd/dd.php'); However, you will find that you cannot find the file gf.php in ff.php, so how to write it? I told you to write this in dd.php: require('.. / ee/gf, PHP '); That's the only way to write it right. Why? Because when it comes to nested references, the relative path of the referenced file depends on the final referenced file!

In short:

No matter what you decide, your files must contain a whole set of files, for example, some of them are more global.php, some of them are more common.php

Suppose the file is in the root directory

Below/global. PHP

In the first line you add a chdir(dirname(s)); // switch to the directory where global.php is located

Other file usage

The require ".. /.. /.. / global. PHP ";
The require "aa/aa. PHP";
The require "bb/bb/cc. PHP";

That should do it, because your global. PHP has already switched the path to wwwroot, and you don't have to go to the trouble of many people

Define (' ROOT_PATH 'dirname (__FILE__));
The require ROOT_PATH...

The chdir (dirname (__FILE__)); It's really nice, relative to where you're going to introduce this file and the other import files are just going to be based on it.


Related articles: