How to get the home directory from a PHP CLI script?

前端 未结 13 1091
生来不讨喜
生来不讨喜 2020-12-08 03:45

From the command line, I can get the home directory like this:

~/

How can I get the home directory inside my PHP CLI script?



        
13条回答
  •  生来不讨喜
    2020-12-08 04:22

    You can rely on "home" directory when working with web server. Working CLI it will give the user path (in windows) if you want your script to work in web and cli environments I've found better to go up any levels with dirname until your root(home) directory.

    echo dirname(__DIR__).PHP_EOL; // one level up
    echo dirname(dirname(__DIR__)); //two levels up
    echo dirname(__DIR__,2).PHP_EOL; //two levels only php >7.0
    

提交回复
热议问题