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

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

    Use $_SERVER['HOME']


    Edit:

    To make it complete, see what print_r($_SERVER)gave me, executed from the command line:

    Array
    (
        [TERM_PROGRAM] => Apple_Terminal
        [TERM] => xterm-color
        [SHELL] => /bin/bash
        [TMPDIR] => /var/folders/Lb/LbowO2ALEX4JTK2MXxLGd++++TI/-Tmp-/
        [TERM_PROGRAM_VERSION] => 272
        [USER] => felix
        [COMMAND_MODE] => unix2003
        [__CF_USER_TEXT_ENCODING] => 0x1F5:0:0
        [PATH] =>/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin
        [PWD] => /Users/felix/Desktop
        [LANG] => de_DE.UTF-8
        [SHLVL] => 1
        [HOME] => /Users/felix
        [LOGNAME] => felix
        [DISPLAY] => /tmp/launch-XIM6c8/:0
        [_] => ./test_php2
        [OLDPWD] => /Users/felix
        [PHP_SELF] => ./test_php2
        [SCRIPT_NAME] => ./test_php2
        [SCRIPT_FILENAME] => ./test_php2
        [PATH_TRANSLATED] => ./test_php2
        [DOCUMENT_ROOT] => 
        [REQUEST_TIME] => 1260658268
        [argv] => Array
          (
            [0] => ./test_php2
          )
    
        [argc] => 1
        )
    

    I hope I don't expose relevant security information ;)

    Windows Compatibility

    Note that $_SERVER['HOME'] is not available on Windows. Instead, the variable is split into $_SERVER['HOMEDRIVE'] and $_SERVER['HOMEPATH'].

    0 讨论(0)
  • 2020-12-08 04:14

    PHP allows you to get the home dir of any of the OS users. There are 2 ways.

    Method #1:

    First of all you gotta figure out the OS User ID and store it somewhere ( database or a config file for instance).

    // Obviously this gotta be ran by the user which the home dir 
    // folder is needed.
    $uid = posix_getuid();
    

    This code bit can be ran by any OS user, even the usual webserver www-data user, as long as you pass the correct target user ID previously collected.

    $shell_user = posix_getpwuid($uid);
    print_r($shell_user);  // will show an array and key 'dir' is the home dir
    
    // not owner of running script process but script file owner
    $home_dir = posix_getpwuid(getmyuid())['dir'];
    var_dump($home_dir);
    

    Documentation

    Method #2:

    Same logic from posix_getpwuid(). Here you gotta pass the target OS username instead of their uid.

    $shell_user = posix_getpwnam('johndoe');
    print_r($shell_user); // will show an array and key 'dir' is the home dir
    
    // not owner of running script process but script file owner
    $home_dir = posix_getpwnam(get_current_user())['dir'];
    var_dump($home_dir); 
    

    Documentation

    0 讨论(0)
  • 2020-12-08 04:14

    Try $_SERVER['DOCUMENT_ROOT'] as your home directory.

    0 讨论(0)
  • 2020-12-08 04:16

    Depends on where you are and what you're trying to do.

    $_SERVER is completely unreliable for a non-server script, BUT $_ENV['HOME'] might be better for a standard shell script. You can always print_r( $GLOBALS ) and see what your environment gives you to play with. phpinfo() also spits out plaintxt when called from a CLI script, and just good ole php -i executed from a shell will do the same.

    0 讨论(0)
  • 2020-12-08 04:17

    You can fetch the value of $HOME from the environment:

    <?php
        $home = getenv("HOME");
    ?>
    
    0 讨论(0)
  • 2020-12-08 04:18

    If for any reason getenv('HOME') isn't working, or the server OS is Windows, you can use exec("echo ~") or exec("echo %userprofile%") to get the user directory. Of course the exec function has to be available (some hosting companies disable that kind of functions for security reasons, but that is more unlikely to happen).

    Here is a php function that will try $_SERVER, getenv and finally check if the exec function exists and use the appropriate system command to get the user directory:

    function homeDir()
    {
        if(isset($_SERVER['HOME'])) {
            $result = $_SERVER['HOME'];
        } else {
            $result = getenv("HOME");
        }
    
        if(empty($result) && function_exists('exec')) {
            if(strncasecmp(PHP_OS, 'WIN', 3) === 0) {
                $result = exec("echo %userprofile%");
            } else {
                $result = exec("echo ~");
            }
        }
    
        return $result;
    }
    
    0 讨论(0)
提交回复
热议问题