Analysis of the Relationship between PHP absolute path and relative path

Source: Internet
Author: User

PHP does not seem to use "/" as ASP does to represent the root directory, replacing it with $ _ server ['document _ root']. Others are the same :.. /indicates the upper layer .. /Indicates the current layer. If A/B/C/S. php needs to call/BB/s2.txt in the root directory, then:

$ Rootdir = $ _ server ['document _ root'];
$ Firedir = "$ rootdir/BB/s2.txt ";

Or: "../BB/s2.txt" indicates to return to B, to a, to the root directory, and then to BB.

Some time ago, I was plagued by the introduction of require () Relative positional relationships to files in PHP development. In order to thoroughly understand their relationships, I made an experiment.

The following is an experiment diagram:

The absolute path of the current project (project2) is: D: \ www \ php_case \ coucom_make. That is, root of the current project.

In order to better express the introduction of different levels of directory files, I boldly divide the reference into three types: superior to underling. (STOU )).

References of lower-level superiors (for short, lower-level references; otherwise)

A level reference is also called a level reference (paratactic ).

Well, we have defined the reference type. Next we will look at the rules for referencing different types.

Let's talk about the reference from the superior:

Let's take a look at the experiment diagram. The project in the figure contains three directories of the same level and one index respectively. PHP file, and CC directory under BB, DD directory and CC under CC. php ccc. two PHP files. The same DD also contains a DD. PHP file. All upper and lower layers are referenced by superiors.

For example, index. php references all files:
Reference of CC. php to dd. php:
Ee. php references dd. php:

If you take a closer look at the directory structure, you will find that although these three references belong to the same parent references, they are not exactly the same. I divide them into two situations: there are two kinds of references in the upper-level reference: 1. subordinate parent reference (class such as CC. PHP for DD. PHP, because these two files belong to the same CC directory) 2. non-subordinate parent reference (class such as index. PHP references all files and EE. PHP for DD. PHP references all belong to this situation because they do not have a common parent directory with the referenced files, which can be relative to the site root directory ).

For reference by subordinate superiors:

The following is a reference to dd. php in CC. php:

Require ('dd/DD. php ');

For non-subordinate superiors:

The following is a reference to CC. php In EE. php:

Require ('../BB/CC. php ');

The above is the upper-level reference. Let's take a look at the lower-level references of the lower-level refer! Similarly, the lower-level references to the upper layer belong to the lower-level references, which are also divided into two categories: subordinate and non-subordinate. The relative paths of the lower-level references use their parent directories as the root directories. For example:

TTT. php references bbff. php in this case: require ('../BB/CC. php ');

A non-subordinate reference is based on the root directory of the website. For example:

CCC. php references ee. php: require ('../EE. php ');

The above is the introduction of lower-level references. Finally, let's take a look at the level-level or same-level references. In fact, the same-level references are also divided into two types: the same-level and non-subordinate references.

It is easy to reference a subordinate peer level: that is, the reference of two files in the same directory

For example, reference TTT. php require ('ttt. php') in DD. php ');

Non-subordinate reference at the same level: that is, it is not in the same directory (there is no common parent directory, only the website as the parent directory), but the level is the reference of the same two files, for example: in AA. PHP references ee. PHP require ('.. /EE. PHP ');

The above three types of references are different, and nested references are also involved.

For example:

Ff. PHP references DD. PHP, While DD. PHP again references GF. PHP. In this case, DD. PHP references GF. PHP is a non-subordinate reference in a lower-level reference. The syntax is as follows: require ('.. /.. /.. /EE/GF. PHP '); and ff. PHP references DD. PHP is a subordinate reference in the upper-level reference. The statement is as follows: require ('. /CC/DD. PHP '), but you will find it in ff. GF cannot be found in PHP. how to Write PHP files? I told you to write in DD. php like this: require ('../EE/GF. php'); it is correct to write in this way. Why? Because when nested references are involved, the relative path of the referenced file must be subject to the final Referenced File!

In short:

In fact, it is very simple. You use your web root directory as the root directory. no matter how you decide, your file must contain all the files, such as some of them are larger than global. PHP, some are more common. PHP

Assume that this file is placed in the root directory.

Wwwroot/global. php

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

Use other files

Require ".../global. php ";
Require "AA/AA. php ";
Require "BB/CC. php ";

That's enough, because your global. php has switched the path to wwwroot, and you don't have to be as troublesome as many people do.

Define ('root _ path', dirname (_ file __));
Require root_path .....

Chdir (dirname (_ file _); It is really good to introduce this file in a relative position. Other import files only need to use it as the benchmark.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.