Php does not seem to use "/" as asp does to represent the root directory, replacing it with $ _ SERVER [
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:
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.
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.