I have several PHP files include()ing other files from several other directories.
In one of those included files: foo/bar.php, I want bar.php to include 3 other files contained in the directory foo. However bar.php is actually included by another file in another directory, which is included by yet another file, and so on.
If I did:
include('./apple.php');
include('./orange.php');
In foo/bar.php, will it include the correct files from the foo directory regardless of which file included bar.php in itself?
No. Using
./at the start of your include file name forces it to be searched from the “current directory” as set by your web server (most probably the directory of the initial script, or the DocumentRoot, depending on the webserver).The way to get the behaviour you want depends on the value of your
include_path(which can be modified withset_include_path()if necessary).From the documentation for
include():So, if there’s no chance that the filename will be found in another directory in the
include_pathfirst, you could useinclude('apple.php').If there is a possibility that apple.php exists elsewhere, and you want the copy in this folder to be used first, you could either use Matthew’s suggestion, and
or, if you have many files to include from the current directory: