views:

247

answers:

2

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;
        }
    }
+1  A: 

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

kolbyjack
Perfect. Do you happen to know a Windows equivalent?
Bart van Heukelom
I've never done any php programming on windows, so no
kolbyjack
After looking through the manual a bit, you may be able to use the W32api ( http://us.php.net/manual/en/w32api.examples-uptime.php ) along with SHGetFolderPath(CSIDL_PERSONAL?) ( http://msdn.microsoft.com/en-us/library/bb762181%28VS.85%29.aspx ) to get the My Documents folder on Windows
kolbyjack
A: 

If safemode is disabled, try this one

$homedir = `cd ~ && pwd`;
Cassy