Solution to the problem of include page path in PHP

黄舟
Release: 2023-03-11 12:00:01
Original
1961 people have browsed it


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
Copy after login


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.

Since relative paths cannot be used, we can use absolute paths instead.

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");

In this case, no matter which level of directory the page is in, I can quote it without having to Worry about the path problem!

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!