I know that this was answered and accepted, but I did not see anyone thinking about why this solution worked and what was wrong in the first place.
Brief Description of the Problem
The current working directory is set at the php point that processes the request, and all relative paths are resolved based on the current working directory, not the directory of the file to which the path is pointed.
Long problem description
Relative paths in php are resolved based on the current working directory.
For testing purposes, you can always see what the current working directory is by calling getcwd
This value is initially sent to the http request as a directory containing the file that the web server originally passed to the request in php.
So, for example, if you go to http://www.mydomain.com/index.php , the current working directory will be the same as the document root ( $_SERVER["DOCUMENT_ROOT"] )
For a CLI request, cwd is the directory you are in when you execute the command. Therefore, if I am in /home/orangepill and I run /usr/bin/php /path/to/file.php , cwd will be /home/orangepill .
This causes a problem for relative file references inside the included files.
Let's look at this example.
The client will go to the site www.mydomain.com
Apache has index.php installed in the DirectoryIndex directive, and apache finds the index.php file in the document root directory. The current working directory is set to the root of the document.
/index.php contains the line include "library/myclass.php"; $ _SERVER ["DOCUMENT_ROOT"]. "/Library/myclass.php" exists and everything is fine
myclass.php contains the line include("myclass_helper.php"); which permits $ _SERVER ["DOCUMENT_ROOT"]. "/myclass_helper.php". (remember that relative links are resolved relative to the current working directory)
$_SERVER["DOCUMENT_ROOT"]."/myclass_helper.php" does not actually exist in $_SERVER["DOCUMENT_ROOT"]."/library/myclass_helper.php"
You are probably, but wait ... I experienced different behaviors in my scripts by including it in the include. The reason for this is that include and require language constructs (along with several other file system commands) try to include relative paths from each of the paths specified in the include php directive. So, in the above example, if the library directory with the document root existed inside the included paths, then everything will work as expected.
A bulk solution for the required files relative to the current file is to structure your include paths using the __DIR__ context constant. So you would use include __DIR__."/myclass_helper.php"; ( include dirname(__FILE__)."/myclass_helper.php in pre PHP 5.3 environments), and at run time it would actually turn your relative path into an absolute path based on the location of the file making the include.
For the usual included directories, Iβm used to setting some frequently used places for use with relative file system links. eg
define ("APPLICATION_PATH", realpath($_SERVER["DOCUMENT_ROOT"]."/application"); define ("LIBRARY_PATH", realpath($_SERVER["DOCUMENT_ROOT"]."/library"); define ("CONFIG_PATH", APPLICATION_PATH."/etc/";
This gives you a lot of reference points for including paths relatively.