Detailed explanation of PHP's require_once path problem
My site directory is as follows:
****************************************** ***************************
wwwroot //The absolute path to the website root directory is: F:/wwwroot
-- folder_a // Folder A
file_a_a.php file_a_b.php file_a_c.php
-- folder_b // Folder B
file_b_a.php file_b_b.php file_b_c.php
-- index.php
******** *************************************************** ***
This directory hierarchy is already very clear:
wwwroot is the root directory, and below there are the index.php file and two folders folder_a and folder_b
There are 3 php files in each folder
Let’s first look at the contents of the index.php file:
<?php require_once("folder_a/file_a_a.php"); echo "文件folder_a_a.php被包含成功"; ?>
Then look at the contents of the folder_a/folder_a_a.php file:
<?php require_once("../folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); ?>
Finally, let’s take a look at the contents of the folder_b/folder_b_a.php file:
<?php class X{ function printInfo(){ echo 'success; } } ?>
ok If I run floder_a/file_a_a.php
directly now, it will output: success
If I When running index.php
under wwwroot, an error will be reported because the included file cannot be found:file_b_a.php
But if I am in all require_once() Add dirname(FILE).'/'
Then whether you run file_a_a.php or index.php, you can output
********** normally ************************************************
Problem:
The first time I used a relative path, so an error occurred when I repeated the inclusion
The second time I used an absolute path, so there was no error . But I am still a little confused:
I first analyzed the following reasons for the error when using relative paths:
I run index.php, it can find the folder_a directory, and it can also find the folder_a directory. file_a_a.php, so it copies the contents of folder_a/file_a_a.php to the first line of index.php (the line containing the statement), and then continues to run (that is, runs the included content), so this It is equivalent to running require_once('../folder_b/file_b_a.php') in file_a_a.php in index.php; It looks for this path file (file_b_a.php) based on the current location of index.php, and of course it cannot find it. Here it is, so it went wrong.
But isn’t it the same when I use absolute paths? But why doesn’t it go wrong? Maybe everyone is a little confused about this sentence, let me explain in detail (according to The program running sequence will be explained).
The program runs index.php first (note that I added dirname (FILE) at this time, so the current is an absolute path),
index.php first Run the first code: require_once(dirname(FILE).'/'.'folder_a/file_a_a.php');
dirname(FILE) is f:/wwwroot/, so the path included in this code is also That is:
f:/wwwroot/folder_a/file_a_a.php
This path is correct, so there is no problem, right?
ok The first step is completed correctly
Then it copies the code in file_a_a.php to this location in index.php:
Then it continues to run: This is all the code in file_a_a.php running in index.php code, then let’s take a look at which codes it runs?
<?php require_once(dirname(FILE).'/'."../folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); ?>
Yes, that’s it. It should be noted that these codes have been copied to index.php, that is to say, the content of index.php is actually now It becomes:
<?php require_once(dirname(FILE).'/'."../folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); echo "文件folder_a_a.php被包含成功"; ?>
Then still analyze the above code according to the execution order of the program:
dirname(FILE) should be f:/wwwroot (because these codes are now in index.php Execution, the same as relative paths, so what you get is the directory where index.php is located)
Then the included path should be: f:/wwwroot/../folder_b/file_b_a.php
So let's check if there is a file_b_a.php file in this path? The answer is no, because f:/wwwroot/../folder_b has returned to the folder_b directory under the f: drive letter, and this directory does not exist.
But the execution result puzzled me. It was output correctly.
Maybe you will say: In the relative path, index.php first contains file_a_a.php and then executes it. Contains the code of file_b_a.php, so the directory cannot be found. After using the absolute path, before index.php includes file_a_a.php, file_a_a.php has already executed the code containing file_b_a.php, so the output is correct. But please note :The PHP documentation and many documentation tutorials explain this: Including a file actually means copying the code in the included file to the place containing the command. Even if this view is wrong, it is also a require_once command, no Maybe the relative path is included first and then executed, while the absolute path is executed first and then included.
I don’t understand, please give me some advice. (Thank you very much for reading this. The question is very long because I've been depressed for a longer time, so please don't Ctrl+C Ctrl+V, and don't say: look at the API or find your own qualifications, etc.) Thank you.
Don't think too complicated...
Use absolute path:
Your c:\a.php
canreference
d:\b.php
The thing you misunderstood is that FILE always points to the current file, whether it is the main executable file or the included file
That is to say: if you use a relative path../folder_b/file_b_a.php Then it will be copied first and then pointed to the address. If you use FILE, it will be pointed to first and then included. Is that so?
If you use a virtual directory, then this directory will be based on your current running directory. The file is the benchmark
which is
folder_a/folder_a_a.php文件的内容这样写:
<?php require_once("folder_b/file_b_a.php"); $x = new X(); $x.printInfo(); ?>
你看下会不会错
在跟目录里建个文件,把要引用的文件全部按绝对路经引进来;其他目录引这个文件就好了.
The above is the detailed content of Detailed explanation of PHP's require_once path problem. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

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)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
