Home > Backend Development > PHP Tutorial > The most effective way to solve the relative directory problem in PHP_PHP Tutorial

The most effective way to solve the relative directory problem in PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 14:53:10
Original
774 people have browsed it

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__)."/../b.php");

include_once(dirname(__file__)."/c.php");

?>
If c.php contains d.php. It is written as follows:
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

http://www.bkjia.com/PHPjc/371464.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371464.htmlTechArticle
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...
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