获取主文件夹

To this day i'm using that piece of code to locate public_html to count the folders to go back in order to import files:

$config_path = array_reverse(explode( DIRECTORY_SEPARATOR , getcwd()));
$co = 0;
foreach($config_path as $val){
    if($val == "public_html")
        break;
    $co += 1;
}
$config_path = '';

if($co != 0){
    for($i = 0 ; $i < $co ; $i++){
        $config_path .= '../';
    }
}

and then i'm using it like that:

require_once ($config_path.'core/db-class.php');

That piece of code serve me pretty good , though I feel like its limiting me because when I use WAMP I need to have public_html folder and such

I'm looking for a php function something like - getMainRoot()

So I can do something like

require_once (getMainRoot().'core/db-class.php');

Use the $_SERVER['DOCUMENT_ROOT'] php variable.

One can always use the $_SERVER['DOCUMENT_ROOT'] but that does not work running the internal web server or from the command line.

If your application has a single entry point like index.php you can make a define there:

define( 'ROOT_PATH', __DIR__ );

and use that from there on.