Solutions to Path Problems in PHP

Source: Internet
Author: User
For the solution to the path problem in PHP, read the solution to the path problem in PHP. bylongnetpro introduction: the inclusion path in PERL and PHP has always been a hard problem to solve, it is mainly related to the operating system and WEB servers. it is impossible to solve this path problem intelligently. Compared with PERL, PHP has a much better path and is much easier to solve. "> <LINKhref =" http :/

By longnetpro

Introduction:
The inclusion path in PERL and PHP has always been a hard problem to solve. it is mainly related to the operating system and WEB server. it is impossible to solve this path problem intelligently. Compared with PERL, PHP has a much better path and is easier to solve, because the relative path of PHP can be used in any PHP program, unlike PERL, which must use absolute paths in some statements, the porting is extremely complex.

Based on this, in PHP, I have designed an absolutely secure solution, as described below.

Principles:
Using relative paths, but using absolute paths in relative paths (a bit around, will be explained later) can ensure portability, and can be easily modified, third, it is formulated with clear structure and easy to expand.

Detailed steps:
1. First, determine the root directory of the program. Note that it is in the file system, not a virtual directory under the WEB server, however, the relative path of the sub-directory under this directory is usually the same as that of the virtual sub-directory under the URL.
2. create a settings under each subdirectory under the defined program root directory (not necessarily each, as needed. php, which defines a variable or constant (the constant is better, because the scope is relatively large), such as APPROOT, but this APPROOT is not an absolute path, it is the relative path of the directory relative to the program root directory you specified.
3. write require_once ('settings. php ');, but note that it is best not to add this sentence to all the contained files. write if (! Defined (APPROOT) define (APPROOT, '.../...'); this type of statements prevents redefinition.
4. if you want to include other files, whether directly or indirectly, you can write include (APPROOT. $ path); here, $ path is the absolute path of the contained file relative to the root directory of the program you specified.

Principle:
The specified 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 to the program root directory. For example, the directory c: \ wwwroot \ app specifies the program root directory for you, and then there are two files c: \ wwwroot \ app \ a \ index. php and c: \ wwwroot \ app \ B \ inc. php. For sub-directory a, APPROOT is '.. ', and for the program root directory, inc. the absolute path of php is $ path = '/B/inc. php, the combination of the two is '.. /B/inc. php '. If you want. php contains inc. php needs to be written as include ('.. /B/inc. php '), and this path is not exactly the combined APPROOT. $ path?


Conclusion:
After the above processing, each path is absolutely uniform, and the only thing that is arrogant is to define this APPROOT under each directory, but each directory only needs to be in the settings of this directory. it is enough to define it once in php. 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, you only need to go to the index. settings. php defines it once. If a friend has worked on the Delphi Project and studied the project file, he will find that the only main entrance file of a program I just mentioned is very similar to that of the Delphi Project, except for one main program File (dpr file), Delphi cannot execute all the other unit files or resource files independently. In PHP, if this happens, you only need to define APPROOT once and write the first sentence in the main program file as require_once ('settings. php '); in the future, you can use include (APPROOT. $ path); to ensure that there is no problem, unless you do not write this "contains 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. You can also refer to the path definition method in WEB-INFO of JSP.

I am using a formula that will remain unchanged. if a friend has a better solution, you are welcome to discuss it! If you have any questions, please submit them.

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.