Solution to path problem in PHP_PHP tutorial
Jul 21, 2016 pm 04:09 PM
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.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

Reasons and solutions for the header function jump failure: 1. There is output before the header, and all pre-outputs need to be checked and removed or ob_start() buffer is used; 2. The failure to add exit causes subsequent code interference, and exit or die should be added immediately after the jump; 3. The path error should be used to ensure correctness by using absolute paths or dynamic splicing; 4. Server configuration or cache interference can be tried to clear the cache or replace the environment test.

The method of using preprocessing statements to obtain database query results in PHP varies from extension. 1. When using mysqli, you can obtain the associative array through get_result() and fetch_assoc(), which is suitable for modern environments; 2. You can also use bind_result() to bind variables, which is suitable for situations where there are few fields and fixed structures, and it is good compatibility but there are many fields when there are many fields; 3. When using PDO, you can obtain the associative array through fetch (PDO::FETCH_ASSOC), or use fetchAll() to obtain all data at once, so the interface is unified and the error handling is clearer; in addition, you need to pay attention to parameter type matching, execution of execute(), timely release of resources and enable error reports.
