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

前端 未结 13 1060
生来不讨喜
生来不讨喜 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

    This function is taken from the Drush project.

    /**
     * Return the user's home directory.
     */
    function drush_server_home() {
      // Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
      // getenv('HOME') isn't set on Windows and generates a Notice.
      $home = getenv('HOME');
      if (!empty($home)) {
        // home should never end with a trailing slash.
        $home = rtrim($home, '/');
      }
      elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
        // home on windows
        $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
        // If HOMEPATH is a root directory the path can end with a slash. Make sure
        // that doesn't happen.
        $home = rtrim($home, '\\/');
      }
      return empty($home) ? NULL : $home;
    }
    

提交回复
热议问题