Got two methods, one for WordPress and another for other general PHP scripts.

 

WordPress; Simply use the constant ABSPATH eg file(ABSPATH.”some-other-folder/some-file.txt”); – it returns the path to where wp-config.php is located for the current wordpress install.

 

General: Can use the following to obtain the home or root folder.. $doc_root = preg_replace(“!${_SERVER[‘SCRIPT_NAME’]}$!”, ”, $_SERVER[‘SCRIPT_FILENAME’]);

 

Note: $doc_root won’t include an end / – so on any additional file/path one will need to be added.