Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read user home directory from PHP

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;
        }
    }
like image 792
Bart van Heukelom Avatar asked Jan 31 '10 21:01

Bart van Heukelom


2 Answers

I think you want the result of either: http://us.php.net/manual/en/function.getmyuid.php or http://us.php.net/manual/en/function.posix-getuid.php sent to http://us.php.net/manual/en/function.posix-getpwuid.php

like image 165
kolbyjack Avatar answered Oct 12 '22 04:10

kolbyjack


If safemode is disabled, try this one

$homedir = `cd ~ && pwd`;
like image 35
Dan Soap Avatar answered Oct 12 '22 02:10

Dan Soap