Imagine I have two directories, foo and bar in a parent directory, and these directories contain some PHP files:
/
/index.php
/foo
/foo/functions.php
/foo/stuff.php
/bar
/bar/doodie.php
Let’s say:
functions.phpincludesstuff.phpindex.phpincludesfunctions.phpdoodie.phpincludesfunctions.php
Based upon this structure, when index.php is run, it will look for /foo/functions.phpin the /foo directory. However, stuff.php must be included using the relative path according to the location of index.php. This works, in the case of when index.php is run. It doesn’t work when doodie.php is called because functions.php looks for stuff.php in the /foo directory, which doesn’t exist when the location is /bar.
Is it possible to include files according to where they reside instead of where they are called from, without using absolute paths (and session variables)?
I apologize if this wasn’t clear, it was really tough to describe this…
You should use absolute paths.
Of course you don’t need to include the full path every time. The usual process is that you define a constant called, for example, APP_PATH and set the absolute path of the application in your bootstrap.
After that, whenever you want to include a file, append the APP_PATH constant in your include calls. This will solve your problem.
You could also look at the include_path option, this depends on how your files are located – http://php.net/manual/en/function.set-include-path.php