Wwwroot//Website root absolute path 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 clear:
Wwwroot is the root directory with index.php files and folder_a,folder_b two folders below
Each of these folders has 3 PHP files
First look at the contents of the index.php file:
The code is as follows |
Copy Code |
<?php Require_once ("folder_a/file_a_a.php"); echo "File folder_a_a.php was included successfully"; ?> |
Look again at the contents of the folder_a/folder_a_a.php file:
The code is as follows |
Copy Code |
<?php Require_once (".. /folder_b/file_b_a.php "); $x = new X (); $x. Printinfo (); ?> |
Finally, let's look at the contents of the folder_b/folder_b_a.php file:
The code is as follows |
Copy Code |
<?php Class x{ function Printinfo () { Echo ' success; } } ?> |
OK if I run floder_a/file_a_a.php right now
Then output: Success
If I run wwwroot under the index.php
Then the error occurs because the include file could not be found: file_b_a.php
But if I add dirname (__file__) to all the require_once (). /‘
Then the normal output can be run either file_a_a.php or index.php
*********************************************************
Problem:
I used the relative path for the first time, so I made a mistake when I repeated the inclusion.
And I used the absolute path for the second time, so I'm not wrong. But I'm still a little puzzled:
I first analyzed the following causes of errors using relative paths:
I run index.php, it can find the Folder_a directory, it can also find the file_a_a.php in the directory, so it copied the contents of folder_a/file_a_a.php to the first line of index.php (the line containing the statement), Then continue running (that is, run the content that contains it), so at this point you run the require_once ('.. file_a_a.php in index.php. /folder_b/file_b_a.php '); It is based on the location of the current index.php to find the path file (file_b_a.php), of course, can not find it, so it went wrong.
But is it not the same when I use the absolute path? But why is it not wrong? Maybe it's a little confusing, I'll explain it in detail (according to the Order of the program).
The program runs index.php first (note that at this point I have added dirname (__file__), so the absolute path is currently),
index.php first sentence code: require_once (DirName (__file__). /‘.‘ Folder_a/file_a_a.php ');
DirName (__file__) is f:/wwwroot/so the code contains the path:
f:/wwwroot/folder_a/file_a_a.php
This path is correct, so no problem, right?
OK the first step is done correctly
It then copies the code in the file_a_a.php to the index.php of this place:
And then continue running: This is all the code in index.php running file_a_a.php, so let's see if it runs that code?
The code is as follows |
Copy Code |
<?php Require_once (DirName (__file__). ' /‘.".. /folder_b/file_b_a.php "); $x = new X (); $x. Printinfo (); ?> |
That's what this is all about, and it's important to note that the code has been copied to index.php, which means that the content of the index.php now actually becomes:
The code is as follows |
Copy Code |
<?php Require_once (DirName (__file__). ' /‘.".. /folder_b/file_b_a.php "); $x = new X (); $x. Printinfo (); echo "File folder_a_a.php was included successfully"; ?> |
Let's take a look.
Assuming the following three files, the c.php a.php b.php corresponding storage directory is: Localhost/localhost/localhost/demo
The code is as follows |
Copy Code |
c.php Require_once ("a.php"); Require_once ("demo/b.php"); B::d emo (); a.php Class A { } |
B.php's content is interesting, because it has to inherit CLASS A, so I put a.php into it.
copy code |
tr>
Require_ Once (".. /a.php "); Class B extends A { public static function demo () { echo "XX"; } } |
Execute localhost/c.php system error, error message as follows
Warning:require_once (.. /a.php) [Function.require-once]: failed to open stream:no such file or directory in f:wwwdemob.php on line 2
Fatal err Or:require_once () [function.require]: Failed opening required '. /a.php ' (include_path= '); C:php5pear ') in f:wwwdemob.php on line 2 However, surprised to find that if the b.php inside the require_once statement, the execution of normal, then must be require_once statement definition more? The reason is that class A has been redefined two times? But it won't. If I only add require_once (' a.php ') to the c.php, this statement, even if I write two times is true, what is the matter?
The reason is that the directory hierarchy of the b.php definition and the c.php execution file is inconsistent, resulting in two require_once statements inside the c.php. Make it equal to
The code is as follows |
Copy Code |
Require_once ("a.php"); Require_once (".. /a.php "); Class B extends A { public static function demo () { echo "XX"; } } B::d emo (); |
The reason was found, because inside the c.php, its relative directory "..." Is the c.php of the previous layer, resulting in files can not find an error.
Therefore, we conclude that in PHP, when using require_once, there are different levels of relationships, and there is relative to the use of the directory must be cautious, careful.
Require_once is very simple to use but when used, we try to use absolute path.
PHP require_once usage and relative directories be cautious