$_SERVER['DOCUMENT_ROOT'] will not be available in the CLI . The web server defines the document root. In the CLI , you are not using a web server, so there is no document root.
You can try to rely on environment variables, assuming they are set by your shell. For example, $_SERVER['PWD'] represents the current directory and $_SERVER['HOME'] represents the user's home directory.
But this example with PWD is irrelevant if you run the script from a different directory than the executable file. That is, PWD reflects the directory in which you are at the time of launch.
I will give my example , which allows you to calculate the directory of the file being launched:
After developing any tool in PHP, the question becomes how resourceful the created code is and what elements it still affects. Let's take a look at xhprof installation, configuration, and how to work with xhprof.
Faced the problem of authorization 1s on the site. For some reason, the server did not accept the PHP_AUTH_USER and PHP_AUTH_PW parameters from it. Let's take a closer look at the solution and bypassing these parameters in $_SERVER .