Home  >  Article  >  Backend Development  >  Solution to path problem in PHP_PHP tutorial

Solution to path problem in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:09:06999browse


Solution to path problem in PHP Introduction:
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.

Principles:
Use relative paths, but use absolute paths within relative paths (a bit convoluted, explained in detail later). First, portability can be guaranteed, second, it can be easily modified, and third, Formulated and clearly structured, it is easily extensible.

Detailed explanation of the 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, in general, the subdirectories under this directory are relatively The path is the same as the virtual subdirectory of the directory under the URL.
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 of the scope Relatively large), 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. In the first sentence of all program entry files in this directory (that is, the first file that contains other files, or the file that allows direct browsing in the browser), write require_once('settings.php ');, but please note that it is best not to add this sentence to all included files - in fact, it can be added, 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. path.

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. path. 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?


Conclusion:
After the above processing, all paths are absolutely uniform. The only thing that is a bit verbose is that the APPROOT needs to be defined in each directory, but only this directory needs to be defined in each directory. Defining it once in settings.php 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 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 any friend has done a Delphi project and studied the project files, they 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 inclusions can be included (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 and the results are very good. 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 and adapts to all 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314623.htmlTechArticleSolution to the path problem in PHP Introduction: The inclusion path in PERL and PHP has always been a difficult problem to solve. The problem is mainly related to the operating system and WEB server. It is impossible to be very smart...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn