php - 从 PHP 脚本获取 Windows 和 Linux 上的主目录的最佳形式是什么?

标签 php windows linux

我需要这样的东西: get_home_user($用户名)

最佳答案

我是这样做的:

class goDirAlias
{
    private function home_dir()
    {
        // Try to find out the home directory of the user running this script
        if(function_exists("posix_getpwnam"))
        {
            // using posix
            $user_info = posix_getpwnam(goDirAlias::whoami());
            $home_dir = $user_info['dir']."/";
        } else 
        {
            // Looking for Windows environment variables
            $home_dir = getenv('HOMEDRIVE').getenv('HOMEPATH').'\\';
            if($home_dir == "\\")
            {
                // Looking for *nix environment variables
                $home_dir = getenv('HOME')."/";
            }
        }

    return $home_dir;
    }

    private function whoami()
    {
        // Try to find out the username of the user running the script
        if(function_exists('posix_getpwuid'))
        {
            // using posix
            $user_info = posix_getpwuid(posix_geteuid());
            $running_user = $user_info['name'];
        } else {
            // Looking for Windows environment variables
            $running_user = getenv('USERNAME');
            if(empty($running_user))
            {
                // Running *nix whoami
                $running_user = exec('whoami');
            }
        }

    return $running_user;
    }
}

关于php - 从 PHP 脚本获取 Windows 和 Linux 上的主目录的最佳形式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658228/

相关文章:

c - 如何修剪SSD磁盘上的 block ?

php - 无法选择 JSON 数据类型的表

php - VTiger 初学者 - 这些元素在哪里?

php - 在 php/mysql 中检测结果集的结尾

Windows PowerShell : changing the command prompt

c++ - 带有文件镜像的 ramdisk

linux - wkhtmltopdf --use-xserver 选项

php - MySQL 查询问题

ruby-on-rails - 为什么错误 "No route matches [GET] "/users/sign_out”?

linux - 第二次从 Proc-FS 文件读取时使用 lseek() 是否安全