After collecting information and multiple tests, we found that in other web programming languages, relative paths are based on the current processing file directory, but this is not the case in PHP. The relative path in PHP is based on the current working directory, not the current processing file directory, which brings a lot of trouble during the development process.
When using include or require to include a file, add dirname (__file__) before the file path. For example: There are currently 4 files a.php, b.php, c.php, d.php, and their relationship is:
b.php (same level as the root directory)
Root directory/a.php
Root directory/c.php
Root directory/dir/d.php
Now a.php should contain b.php and c.php. It is written as follows:
include_once(dirname(__file__)."/../b.php"); include_once(dirname(__file__)."/c.php"); ?> |
include_once(dirname(__file__)."/c.php");
?>
include_once(dirname(__file__)."/dir/d.php"); ?> |
include_once(dirname(__file__)."/dir/d.php"); ?> |
This method is currently the most effective way to solve path misalignment
After collecting information and testing many times, we found that in other web programming languages, relative paths are based on the current processing file directory. as a benchmark, which is not the case in php. The relative path in php is based on the current...