Introduction:
The inclusion path in Perl and PHP has always been a difficult problem, mainly related to the operating system and Web servers, and it is impossible to be very intelligent to solve this path problem. The path to perl,php is much better and easier to solve, because PHP's relative path is available in any situation in the PHP program, unlike Perl, which has to use absolute paths in some statements to be extremely complicated.
Based on this, in PHP, I designed an absolutely secure solution, as described below.
Principle:
Using the relative path, but in the relative path with absolute path (a bit around, to be explained later), one can guarantee portability, two can be easily modified, three is formulated and structure clear, easy to expand.
Detailed steps:
1, first determine a good program of the root directory, note that the file system, not the Web server under the virtual directory, but in general, the directory under the relative path of subdirectories and URL under the directory of the virtual subdirectory is the same.
2, in the well-defined program root directory under each subdirectory (in fact, not necessarily each, As needed, create a settings.php that defines a variable or constant (constant is better because the scope is larger), such as AppRoot, but this approot is not an absolute path, but rather the relative path of the directory relative to the program root you specify.
3. All the program entry files in this directory (that is, the first file that contains other files, or files that allow direct browsing in the browser), write the first sentence require_once (' settings.php '), but note that all included files are best not to add this sentence-- In fact, you can also, because you can write in the settings.php if (!defined (AppRoot)) define (AppRoot, ' ...). /..'); This type of statement prevents redefinition.
4, if you want to include other documents, whether directly or indirectly included, can be written as include (AppRoot. $path), here $path the included file relative to the program root you specify the absolute path.
Principle:
The set of program root directory is relative path, but the specific directory location is relative to that root directory absolute path, the combination of the two is the specific file relative to the program root directory relative path. For example, the directory C:\wwwroot\app for you to specify the program root directory, and then there are so two files c:\wwwroot\app\a\index.php and c:\wwwroot\app\b\inc.php. For subdirectory A, the AppRoot is '.. ' And for the program root, inc.php's absolute path is $path= '/b/inc.php ', which is combined as '. /b/inc.php '. If you want to include inc.php in index.php, write include (' .... /b/inc.php '), and this path is not just a combination of approot. $path?
Conclusion:
Through the above processing, each path is absolutely uniform, the only wordy point is that each directory to define this approot, but each directory only in the settings.php of this directory to define once is enough. If your entire program has only one entry file, such as index.php, and all other files are directly or indirectly included in this unique entry file, just define it once in the settings.php of the index.php directory. If a friend has done Delphi Project and studied the engineering documents, you'll find that I just said a program with only one main entry file is very similar to the Project Delphi, because Delphi except for a main program file (DPR file), the rest is all unit files or resource files, cannot be executed independently. In PHP, if this happens, simply define a approot and write the first sentence in the main program file as require_once (' settings.php '), and all subsequent inclusions can be used with include (AppRoot. $path); There will be no problem unless you do not write this "contains the file relative to the program root directory absolute path" $path.
I used this method more than once, and it worked well. You can also refer to the way the path is defined in the web-info of the JSP.
I this is the formulation of the status quo formula, if there are friends have a better plan, welcome to propose a discussion! If you do not understand, you are welcome to propose.