get root path of linux hosting

im trying to get the main path to use it in my code insted of using this way

include "../../../lib/phpspreadsheet/vendor/autoload.php";

and call it like this way

include $root_path . "lib/phpspreadsheet/vendor/autoload.php";

and im using this way to get $root_path

$username = posix_getpwuid(posix_geteuid())['name'];

echo $root_path = "/home/" . $username ."/public_html/"; // root path

so is that way good? or wrong or bad?

im using linux hosting with php 8.1v

  • It depends. Having a absolute path is what I would prefer. Maybe your provider delivers an ENV variable you could use instead.

    – 

  • @MarkusZeller i can use $_SERVER[‘DOCUMENT_ROOT’]; insted of that?

    – 

  • Sure. The $_SERVER superglobal is perfect for that. But you should check it before using, because if the config changes your application will break.

    – 

Leave a Comment