by longnetpro
Quote:
The inclusion path in PERL and PHP has always been a difficult problem, mainly related to the operating system and WEB server. It is impossible to solve this path problem very intelligently. Compared with PERL, PHP's path is much better and is much easier to solve, because PHP's relative path can be used on any occasion in a PHP program, unlike PERL where absolute paths must be used in certain statements, leading to transplantation. is extremely complex.
Based on this, in PHP, I designed an absolutely solid solution, as described below.
in principle:
Use relative paths, but use absolute paths within relative paths (a bit convoluted, explained in detail later). First, it can ensure portability. Second, it can be easily modified. Third, it is formulaic and has a clear structure and is easy to expand.
Detailed steps:
1. First determine the root directory of a program. Note that it is under the file system, not the virtual directory under the WEB server. However, generally the relative path of the subdirectory under the directory is the same as the virtual subdirectory under the URL. of.
2. Create a settings.php in each subdirectory under the defined program root directory (actually not necessarily every one, depending on the need), and define a variable or constant in it (constants are better because the scope is larger) , such as APPROOT, but this APPROOT is not an absolute path, but a relative path of the directory relative to the program root directory you specified.
3. Write require_once('settings.php'); in the first sentence of all program entry files in this directory (that is, the first file that contains other files, or files that allow direct browsing in the browser); , but please note that it is best not to add this sentence to all included files - in fact, it is okay to add it, because you can write if(!defined(APPROOT)) define(APPROOT, '../. .'); This type of statement prevents redefinition.
4. If you want to include other files, whether directly or indirectly, you can write include(APPROOT.$path);, where $path is the absolute path of the included file relative to the program root directory you specify.
principle:
The determined program root directory is a relative path, but the specific directory location is an absolute path relative to that root directory. The combination of the two is the relative path of the specific file relative to the program root directory. For example, the directory c:wwwrootapp is the program root directory you specified, and then there are two files c:wwwrootappaindex.php and c:wwwrootappbinc.php. For subdirectory a, APPROOT is '..', and for the program root directory, the absolute path of inc.php is $path='/b/inc.php', and the combination of the two is '../b /inc.php'. If you want to include inc.php in index.php, you must write include('../b/inc.php');, and isn't this path just the APPROOT.$path that was just combined?
in conclusion:
After the above processing, each path is absolutely neat and uniform. The only thing that is a bit cumbersome is that the APPROOT must be defined in each directory, but it only needs to be defined once in the settings.php of this directory in each directory. If your entire program has only one entry file, such as index.php, and all other files are directly or indirectly included in this only entry file, you only need to add settings.php in the directory where index.php is located. Define it once and it's OK. If a friend has done a Delphi project and studied the project files, he will find that the situation I just mentioned that a program has only one main entry file is very similar to the Delphi project, because Delphi has only one main program file (dpr file) , the rest are all unit files or resource files, and cannot be executed independently. In PHP, if this situation occurs, you only need to define APPROOT once, and write require_once('settings.php'); in the first sentence of the main program file, and all subsequent includes can use include(APPROOT.$ path);, it is guaranteed that there will be no problems, unless you will not write this "includes the absolute path of the file relative to the program root directory" $path.
I have used this method more than once, with good results. In addition, you can also refer to the way the path is defined in JSP's WEB-INFO.
This is a formulaic plan that remains unchanged in response to ever-changing changes. If anyone has a better plan, please feel free to discuss it! If there is anything you don’t understand, please feel free to ask.