I didn’t find any problems when I first started using the includeinclude file of php,
slowly Later I discovered a very serious problem, for example:
<web>(网站根目录) ├<A>文件夹 │ │ │ └1.php ├<B>文件夹 │ │ │ └2.php └index.php
introduced the B directory through include("../B/2.php"); in 1.php The 2.php file under
Introduce the 1.php file under the A directory through include("A/1.php"); in index.php
Of course there will be a problem when running it and it cannot be found. ./B/2.php file
Calm down and analyze it carefully. Index.php references the 1.php file in the A directory. At this time,
1.php is compiled into Executed in index.php, which is equivalent to 1.php being located in the root directory of the website like index.php
But don’t forget to include ("../B/2.php") in 1.php. ;
What does "../" mean? In the upper-level directory, 1.php is now in the root directory. If you go up one level at this time, 2.php will no longer be found, so the problem arises here.
So how to solve the problem? Many people will think of include("/B/2.php"). This is not good, but it is also not possible.
php is different from our
jsp, in include The "/" used in is not the root directory of the website as we imagined, it represents the current directory, so it still doesn't work.
Is there no solution? Of course there are.
realpath("./") is used to obtain the absolute path of the current website root directory, such as: c:\wamp\www\website name\
So we can pass include("../B/2. php"); changed to include(realpath("./")."B/2.php");
The above is the detailed content of Solution to the problem of include page path in PHP. For more information, please follow other related articles on the PHP Chinese website!