PHP Conference Japan 2024

posix_getgrgid

(PHP 4, PHP 5, PHP 7, PHP 8)

posix_getgrgid根据组 ID 返回组信息

描述

posix_getgrgid(int $group_id): array|false

获取指定 ID 的组信息。

参数

group_id

组 ID。

返回值

返回的数组元素为

组信息数组
元素 描述
name name 元素包含组的名称。这是一个简短的,通常小于 16 个字符的组“句柄”,而不是真实的完整名称。
passwd passwd 元素包含组的密码,以加密格式存储。例如,在使用“shadow”密码的系统上,通常会返回星号代替。
gid 组 ID,应该与调用函数时使用的 group_id 参数相同,因此是冗余的。
members 包含该组中所有成员的 array 类型 string
如果失败,则函数返回 false

范例

示例 #1 posix_getgrgid() 的用法示例

<?php

$groupid
= posix_getegid();
$groupinfo = posix_getgrgid($groupid);

print_r($groupinfo);
?>

以上示例将输出类似以下内容

Array
(
    [name]    => toons
    [passwd]  => x
    [members] => Array
        (
            [0] => tom
            [1] => jerry
        )
    [gid]     => 42
)

参见

添加注释

用户贡献的注释 4 条注释

tech at dslip dot com dot au
22 年前
好的...

此代码应仅被视为一个选项,它在我的环境下有效,仅此而已。以下函数将根据提供的组 ID 返回组的名称

function RC_posix_getgrgid($gid)
{
$LocationGroup = "/etc/group"; //如果您有不同的操作系统,请编辑此项。我的是 Debian
$fp = fopen ("/etc/group","r");
while ($groupinfo = fscanf ($fp, "%[a-zA-Z0-9]:x:%[0-9]:%[a-zA-Z0-9]\n"))
{
list ($name, $groupID, $nfi) = $groupinfo;
if ($groupID == $gid)
{
$returnval = $name;
}
}
fclose($fp);
if($returnval) { return $returnval; } else { return 0; }
}
cweiske at php dot net
15 年前
当 posix_getgrgid() 失败(例如,无效/未知的组 ID)时,它会返回 false。
但 Mac OSX 除外,它会返回一个名称为“nogroup”且 gid 为“-1”的数组。
james at jfc dot org dot uk
21 年前
在 php-4.3 中,返回的数组似乎发生了变化。

它现在返回

["name"] 组名称
["passwd"] 组密码
["members"] 组成员(用户名数组)
["gid"] 数字组 ID
rcgraves+php at brandeis dot edu
24 年前
返回一个包含组结构元素的数组。该数组既有数字索引,每个索引都是一个命名组成员的字符串,也有命名字符串索引。数组元素为

$_["name"] 字符串组名(用户)
$_["gid"] 整数 gid 号码(例如,wheel/root 的 gid 为 0)
$_["members"] 组中用户的数量
$_[0]..$_[n] 组中的用户名
To Top