Search code examples
phpincludeinclude-path

PHP - Best way to use above directory includes


I'm having some problems with some parts of my website are accessing some files from different roots and the includes end sometimes like this:

require_once ('../eggs/libs/lagger/lagger_config.php');
require_once ('../eggs/libs/lagger/lagger_init.php');

and sometimes like this:

require_once("../libs/lagger/lagger_config.php");
require_once("../libs/lagger/lagger_init.php");

is there a better way to solve this problem, without having to use the:

../../

Solution

  • Best way is to get the base directory, i.e.

    define("ROOT",$_SERVER["DOCUMENT_ROOT"]);
    define("LAGGER",ROOT."/lagger/"):
    

    then:

    require_once(LAGGER."lagger_config.php");
    require_once(LAGGER."lagger_init.php");
    

    P.s. I suggest this as you can then use ROOT to build up paths that will be consistent.