In php, if i have a file that requires a file from a subdirectory like:
require('directory/file.php');
and file.php
wants to require another file in its own directory, is the path relative to file.php
or the file that it is included in?
In php, if i have a file that requires a file from a subdirectory like:
require('directory/file.php');
and file.php
wants to require another file in its own directory, is the path relative to file.php
or the file that it is included in?
It's relative to the original requiring file if that makes sense.
So, if you have a file called index which looks like this:
require("./resources/functions.inc.php");
And then functions.inc.php would need to look like this:
require("./resources/anotherFunctionsFile.inc.php");
Rather than:
require("anotherFunctionsFile.inc.php);
But really you should be making use of the __DIR__
constant, which is always the directory the script is being ran from; it makes things a lot easier.
More information about __DIR__
and other constants: http://php.net/manual/en/language.constants.predefined.php
I hope that helps.
It's relative to the main script, in this case A.php. Remember that require()
just inserts code into the currently running script.
That is, does it matter which file the
require()
is called from
No.
If you want to make it matter, and do an require()
relative to B.php, use the __FILE__
constant (or __DIR__
since PHP 5.3) which will always point to the literal current file that that line if code is located in.
include(dirname(__FILE__)."/C.PHP");
include(dirname(__FILE__).'/include.php');
REF : http://php.net/manual/en/language.constants.predefined.php
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.