1.__file__
__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.
This method is the preferred recommendation. Although your include statement will therefore write longer, but a word, value!
<? PHP include dirname (__file__). ' /subdir '; // dirname return value does not contain the trailing slash?>
2.$_server[' Document_root ']
This method allows the Specify a path relative to the Web server Doc_root for file inclusion.
This is also a good way for many projects to be adopted.
<? PHP if (! defined ("Wetsite_base_dir")) Define $_server [' Document_root ']. ' /clare/'); require_once (Wetsite_base_dir. ' includes/global.inc.php ');? >
3.chdir ()
the include looks for file relative to current working directory. We can use the This feature. It's really a "fancy", but I'm not sure whether it's safe all the time. Who knows?
this way feels a little bit troublesome, it's not easy to remember to restore the working directory at any time. After writing this sentence, I then wrote several test files and found that the most important disadvantage of this approach was not trouble, but in its side effects: Changing the working directory, which could lead to procedural logic errors.
<? php $prewd = GETCWD (); // Get the current working directory chdir (realpath (dirname (__file__ )); // working directory to the location of this file include (' includedfile.php '); // include relative to the This file chdir ( $prewd ); // ?
4.set_include_path ()
This is the most convenient way, but not without the disadvantage. First of all, sometimes you don't necessarily have permission to modify the configuration. Second, you will be confused (even if you don't, your maintainer) when the filenames are duplicated under different paths.
5.auto_prepend_file and Auto_append_file in php.ini
This is almost the best approach if you need to include a generic script for each script, but the downside is that it's not configuration-dependent enough to be independent.
Include path modification in PHP