Problems with include and require absolute paths and relative paths in PHP
Oct 19, 2016 am 10:19 AMWhen writing PHP programs, you often use include or require to include other files. However, when there are too many files included in each file, path problems will occur.
The following directory:
├Folder
│ │
│ └1.php
├Folder
│ │
│ └2.php
└index.php
Now the index.php in the root directory needs to include the 1.php file in the A folder, just use include "./A/1.php"
And 1.php in folder 1 includes 2.php in folder B, then write include "../B/2.PHP" in 1.php
But you must know that when index.php includes 1.php, compilation is performed in index.php, that is, the include in the file included in index.php is relative to index.php, then 1.php is included in index.php, then we need to find 2.php relative to index.php. As mentioned above, what is written in 1.php is include "../B/2.php". Now the compiled file is relative to the root directory of the website (that is, relative to index.php), and "../" means Then we have to go back to the upper level directory to search, so how can we find it?
I have also searched for some methods on the Internet, and the best way is to use the absolute path method. You can define a single entry file, include the files to be included, and define a constant define("__ROOT__",dirname(__FILE__));, then in the process of writing subsequent files, you only need to use the absolute method and add __ROOT__ That's it.

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)