PHP: include file relative to calling file NOT root file?
Question
OK say I have a file called fileA.php
in the same directory I have another file called fileB.php
so inside fileA there is this require_once('fileB.php')
notice I did not specify a file path because both files are in the same directory.
Now if fileA is included into another file that is located somewhere else, it fails to include fileB because the parent file (the file that included fileA) is not in the same directory as fileB.
So how can I make it so that fileA loads fileB no matter where fileA is included into?
Does that make sense?
Thanks!!
Solution
Assuming both fileA.php and fileB.php are guaranteed to be in the same directory, this should work:
// fileA.php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR .'fileB.php')
OTHER TIPS
http://php.net/manual/en/function.include.php#78716
You can set the include root to your website root.