问题
Let me demonstrate my file structure first.
/www/
myfile.php
anotherC.php
a/
b.php
c.php
The code inside myfile.php
is:
<?php
include_once("a/b.php");
?>
The code inside b.php
is:
<?php
include_once("c.php");
?>
And finally inside c.php
:
<?php
echo "hello i'm C.php";
?>
So, when I call www/myfile.php
I get output:
hello i'm C.php
These works fine. But let me change b.php
to
<?php
include_once("../anotherC.php"); //or include_once("./c.php"); (it won't work too)
?>
Now, when I call www/myfile.php
, i get Error:
Warning: include_once(../anotherC.php): failed to open stream: No such file or directory in /home/hasib/Desktop/www/a/b.php on line 2 Warning: include_once(): Failed opening '../anotherC.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/hasib/Desktop/www/a/b.php on line 2
Now my question is, why The include_once("c.php");
worked perfectly??
回答1:
the document:
If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
the 'calling script' in your example is b.php , obviously it's directoy is '/www/a/'.
you can use getcwd() to get the 'current directory', either in myfile.php or b.php ,it will return '/www/'
so when include_once("c.php"); it first look up c.php in calling script's directory,that is /www/a/ , and get c.php successfully.
when include_once("../anotherC.php"); , it only look up anotherC.php in relative path to current directory, current directory is /www/ , so it look up anotherC.php in / , /anotherC.php doesn't exists and throw warning.
回答2:
Includes with relative paths are always done relative to the MAIN script. include()
operates essentially the same way as if you'd cut 'n pasted the included data directly into the main script. So when your sub-includes are performed, they're using the working directory of your myFile.php
script, NOT the working directory of b.php
or c.php
.
Your sub-scripts would need to have an absolute path in their icnldues, or at least some kind of "where the heck am I" determination code, e.g. include(__FILE__ . 'c.php')
回答3:
The only reason I can think of for this working is that you have /www/a
in your include_path
. This means that include_once("c.php")
would first look for /www/c.php
(since that's the current working directory), then look for /www/a/c.php
which would be found and work.
However, include_once("./c.php")
explicitly states to look in the current working directory only, and of course since the file is not there, it won't work.
来源:https://stackoverflow.com/questions/21312250/why-two-level-php-include-only-work-on-current-directory