Read user home directory from PHP

Bart van Heukelom picture Bart van Heukelom · Jan 31, 2010 · Viewed 8.2k times · Source

I cannot find a way to get the user's home directory (e.g. /home/jack; whatever ~ in bash points to) in PHP using CGI (suPHP). The $_ENV array is empty, and getenv('HOME') returns nothing.

The reason I want to do this is that in absense of configuration saying otherwise, I want to find variable files used by my application in /home/user/.myappnamehere, as most Linux applications do.


I've built something, but it's not the best; While it works, it assumes a lot about the system (e.g. the presence of /etc/passwd)

 $usr = get_current_user();
    $passwd = file('/etc/passwd');
    $var = false;
    foreach ($passwd as $line) {
        if (strstr($line, $usr) !== false) {
            $parts = explode(':', $line);
            $var = realpath($parts[5].'/.report');
            break;
        }
    }