Analysis _php skills of the relationship between absolute path and relative path in PHP

Source: Internet
Author: User
Tags parent directory

PHP does not seem to be the same as the ASP "/" to represent the root directory, replaced by $_server[' Document_root ', others are the same: ... /represents an upward layer./represents the current layer. If a/b/c/s.php now wants to invoke the/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 and then up to the root directory and then to the BB.

A while ago, always by PHP development, the file introduced require () relative position relationship, in order to thoroughly understand their relationship, the author did an experiment.

The following is an experimental diagram:

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

Subordinate reference to the superior (hereinafter referred to as subordinate, English vice versa)

Peer reference or peer reference (English: paratactic).
 
Well, we've identified the reference type, so let's look at the rules for different types of references.

Let's start by saying the superior quote:

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

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

After a careful look at the directory structure, you will find that these three references, although they belong to the superior reference, but they are not exactly the same, I divide it into two situations: that is, there are two references in the ancestor reference: 1. Subordinate ancestor references (classes such as cc.php to dd.php, since 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 the case because they do not have a common parent directory with the referenced file, in relation 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 mentioned is the superior reference, below we come to understand the subordinate reference! The lower-level references to the upper layers are subordinate references, which are also grouped into subordinate and non-subordinate categories, and subordinate reference relative paths are rooted in their parent directory, for example:

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

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

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

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

A subordinate sibling reference is simple: a reference to two files in the same directory

For example: ttt.php require (' ttt.php ') is quoted in dd.php;

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

These are the three types of references in different situations, as well as problems involving nested references

Like what:

Ff.php refers to dd.php, and dd.php references gf.php, in which case the dd.php reference gf.php a non subordinate reference in the subordinate reference: Require (' ..... /.. /.. /ee/gf.php '), and the ff.php reference dd.php a subordinate reference in a superior reference, written in this way: require ('./cc/dd/dd.php '); But you will find in the ff.php can not find gf.php file, then how to write it? I told you it should be written in dd.php: Require ('. /ee/gf.php '); the only way to write it right, why? Because the relative path of the referenced file is subject to the final reference file when it comes to nested references!

Nutshell:

In fact, you use your Web root to do the root directory. No matter what you decide, you must have a file that contains all the documents, such as some global.php, some more common.php

If this file is put in the root directory

wwwroot/global.php

The first line inside you add a chdir (dirname (__file__)); Switch to global.php directory 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__)); It's really good, the relative position of introducing this file to other introduction files as long as he is 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.