Software > PHP

require() and include() in symlinked scripts

If a php script is invoked via a symlink, files in the target directory of the symlink can be used in require() and include() without having the target directory in php's include_path.

The reason (apparently) is the following. When fopen() tries to open a relative pathname, it operates relative to the actual location of the script rather than the location of the symlink.

Example: suppose directory "a" contains two files, a1.php and a2.php, and a1.php has a require("a2.php") statement, and there is also symlink b1.php in some other directory b pointing to a/a1.php. When b1.php is invoked, file "a2.php" will be included (regardless of the setting of include_path). Moreover, the include() and require() statements in a1.php will not find any files in directory b (when invoked as bi.php) unless include_path contains ".". (That is, the "." in include_path matches the location of the symlink.)

Site Index | Zoom | Admin
URL: https://wilcoxen.maxwell.insightworks.com/pages/39.html
Peter J Wilcoxen, The Maxwell School, Syracuse University
Revised 09/30/2004