An analysis of the relationship between absolute path and relative path of PHP _php tutorial

Source: Internet
Author: User
PHP does not seem to use "/" as the ASP to represent the root directory, instead of $_server[' Document_root '), the others are the same:. /indicates a level up:/represents the current layer. If a/b/c/s.php now wants to invoke/bb/s2.txt under the root directory, then:

$RootDir = $_server[' document_root ');
$fireDir = "$RootDir/bb/s2.txt";

Or: ". /.. /.. /bb/s2.txt "means to return up to B and then up to a again up to the root directory and then to the BB.

A while ago is always affected by PHP development, documents to each other to introduce require () relative position relationship, in order to thoroughly understand their relationship, the author made an experiment.

Here is the experiment diagram:

The absolute path to the current project (PROJECT2) is: D:\www\php_case\Coucom_make. This is the root directory of our current project.
  
In order to more clearly describe the different levels of the introduction of the directory files, I dare to divide the reference into three types namely: superior to subordinate reference (referred to as superior, English translation: superior to underling. Abbreviation (Stou)).

Subordinate references to superiors (referred to as subordinate, English vice versa)

A peer reference or sibling reference (English: paratactic).
 
Well, we've clarified the reference types, so let's look at the different types of rules that reference them.

Let's start with a quote from the superior:

Look at our experimental map, in the project under the program has a AA BB ee three siblings and a index.php file, under BB and CC directory, CC and DD directory and cc.php ccc.php two files, the same DD also contains a dd.php file. All upper-level to the lower layer is a superior reference.

For example: index.php references to all files:
CC. PHP references to dd.php:
ee.php references to dd.php:

Look at the directory structure, you will find that these three kinds of references, although they belong to the superior reference, but they are not exactly the same, I divided it into two cases: that there are two references in the superior reference situation: 1. Subordinate superior references (classes such as cc.php to dd.php, because these two files belong to the CC directory) 2. Non-subordinate ancestor references (classes such as index.php references to all files and ee.php references to dd.php are in this case because they do not have a common parent directory with the referenced file, which can be relative to the site root directory).

For subordinate ancestor references:

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

Require (' dd/dd.php ');

For non-subordinate ancestor references:

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

Require ('.. /bb/cc/cc.php ');

Above is referred to the superior reference, let us look at the subordinate reference! Similarly, the lower-level reference to the upper layer is subordinate to the subordinate and non-subordinate two classes, subordinate subordinate reference relative path to their parent directory as root directory, for example:

Ttt.php's reference to bbff.php is in this case: require ('.. /bb/cc/cc.php ');

Non-subordinate subordinate references are based on the site's root directory, for example:

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

The above is the introduction of subordinate references, and finally we look at the peer reference or peer reference, in fact, the peer reference is also divided into two cases: subordinate sibling and non-subordinate peer reference

Subordinate sibling references are simple: a reference to two files in the same directory

For example: Refer to ttt.php require (' ttt.php ') in dd.php;

Non-subordinate sibling references: that is, not in the same directory (there is no common parent directory, only the site is the parent directory), but the level is the same two file references, for example: in aa.php in reference ee.php require ('. /ee/ee.php ');

These are three types of references in different cases, and there are issues related to nested references

Like what:

ff.php references dd.php, and dd.php also refers to the gf.php, in this case, the dd.php reference gf.php belongs to a non-subordinate reference in the subordinate reference, the wording is this: require ('. /.. /.. /ee/gf.php '), while the ff.php reference dd.php belongs to a subordinate reference in the superior reference, the notation is this: require ('./cc/dd/dd.php '); But you'll find that you can't find the gf.php file in ff.php, so how do you write it? I tell you it should be written in dd.php: Require ('. /ee/gf.php '); that's the only way to write it right. Because the relative path of the referenced file is subject to the final reference file when it comes to nested references!

Nutshell:

In fact, it is very simple, you use your Web root directory to do the root directory. No matter how you decide, you must have a file of all the files to be included, such as some more global.php, some more common.php

If this file is placed in the root directory,

wwwroot/global.php

Inside the first line you add a chdir (dirname (__file__)); Switch to the directory where global.php is the directory

Other files used

Require ". /.. /.. /global.php ";
Require "aa/aa.php";
Require "bb/bb/cc.php";

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

Define (' Root_path ', DirName (__file__));
Require Root_path .....

ChDir (DirName (__file__)); Indeed, the relative position of the introduction of this file other introduction files as long as he is a benchmark.

http://www.bkjia.com/PHPjc/321391.html www.bkjia.com true http://www.bkjia.com/PHPjc/321391.html techarticle PHP does not seem to use "/" as the ASP to represent the root directory, instead of $_server[' Document_root '), the others are the same:. /indicates a level up:/represents the current layer. If a/b/c/s.php now want to ...

  • 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.