In PHP, filegroup() returns the numeric group ID that owns a file. On Linux and macOS this is the group the file belongs to.
Every file on a Unix-like system has an owner and a group, and filegroup() gives you the group as a number. It is useful for permission checks, and you can turn the ID into a name with posix_getgrgid().
Syntax
Call filegroup() with the values you need. Start with this line.
PHP
$groupId = filegroup('data.txt');
Call
What it does
filegroup('data.txt')
Returns the numeric group ID of data.txt.
posix_getgrgid(filegroup('data.txt'))
Turns the group ID into group details, including the name (needs the POSIX extension).
filegroup($a) === filegroup($b)
Checks whether two files belong to the same group.
The result is a number, not a group name. On Windows the value is always 0, so the function is mainly useful on Linux and macOS. PHP caches the result, so call clearstatcache() if the group may change while the script runs.
Library declarations
PHP
filegroup(string $filename): int|false
Parameters
Parameter
Description
filename
Required. The path to the file or directory.
Return Value
Return Value
Description
int
The numeric group ID of the file.
false
Returned on failure, for example when the file does not exist. A warning is also raised.
PHP Examples
Example 1 — Get the Group ID of a File
Create a file and check that filegroup() returns a whole number for it.
PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_filegroup.txt';
file_put_contents($path, 'Hello');
$groupId = filegroup($path);
echo 'Group ID is an int: ', var_export(is_int($groupId), true), PHP_EOL;
unlink($path);
Output
Group ID is an int: true
How It Works
file_put_contents() creates a demo file in the temporary folder.
filegroup() returns the group ID that owns the file as an integer.
The exact number depends on your system, so the example checks its type.
is_int() confirms the result is a number, and unlink() removes the file.
Example 2 — A File That Does Not Exist
For a missing file, filegroup() returns false and raises a warning. Check the result before you use it.
Both files are created by the same PHP process in the same folder.
filegroup() returns the group ID of each file.
The strict comparison === checks that the two numbers are identical.
Both demo files are deleted at the end with unlink().
Frequently Asked Questions
It returns the numeric ID of the group that owns a file. It returns false if the information cannot be read.
Use posix_getgrgid(filegroup($file)). It returns an array that includes the group name. This needs the POSIX extension, which is not available on Windows.
It runs, but the result is always 0 because Windows does not use Unix groups. It is mostly useful on Linux and macOS.
filegroup() returns the group ID of the file, while fileowner() returns the user ID of its owner.
The file does not exist or PHP cannot read its information. Check the path and the folder permissions.