- Where does include look for file given as a relative path?
(from PHP manual)
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.
__FILE__ always equals to the real path of a php script regardless whether it’s included.
__FILE__ helps you specify the file to include using relative path to the including file.
//dirname return value does not contain the trailing slash
This method allows you to specify a path relative to the web server doc_root for file inclusion.
The include looks for file relative to current working directory. We can use this feature. It’s really a "fancy" way, but I’m not sure whether it’s safe all the time. Who knows?
rainfalling at yahoo dot com (21-Sep-2005 01:06)
This is yet another way to include files relative to the current file. I find it easier if you have a lot of includes.
$prewd = getcwd(); // get the current working directory
chdir(realpath(dirname(__FILE__))); // change working directory to the location of this file
include(‘includedfile.php’); // include relative to this file
chdir($prewd); // change back to previous working dir
This way is the most convenient way but it’s not without flaws. First, not in all cases you have permission to change server configuration. Second, if there are many path specified in include_path, the actually included file may not be the one you expected because there may be files of the same name under different directories.
- auto_prepend_file and auto_append_file in php.ini
This almost the best way if your scripts commonly need a startup script. We can do a lot of useful things in the startup script, for examples, define constants, load configurations. But it’s not always OK to change the php.ini settings. Remember the most adaptive application should be as independent from configs as possible.