Home >Backend Development >PHP Tutorial >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!