PHP filegroup() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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');
CallWhat 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

ParameterDescription
filenameRequired. The path to the file or directory.

Return Value

Return ValueDescription
intThe numeric group ID of the file.
falseReturned 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);

How It Works

  1. file_put_contents() creates a demo file in the temporary folder.
  2. filegroup() returns the group ID that owns the file as an integer.
  3. The exact number depends on your system, so the example checks its type.
  4. 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.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_missing_group.txt';

$groupId = @filegroup($path);

if ($groupId === false) {
    echo 'Cannot read the group', PHP_EOL;
} else {
    echo 'Group ID: ', $groupId, PHP_EOL;
}

How It Works

  1. The path points to a file that does not exist.
  2. filegroup() cannot read file information, so it returns false.
  3. The @ operator hides the warning so the script stays quiet.
  4. Use === false, because a group ID of 0 is a valid result, especially on Windows.

Example 3 — Compare the Group of Two Files

Two files created by the same script in the same folder normally belong to the same group.

PHP
<?php
$a = sys_get_temp_dir() . '/codetofun_group_a.txt';
$b = sys_get_temp_dir() . '/codetofun_group_b.txt';

file_put_contents($a, 'A');
file_put_contents($b, 'B');

$same = filegroup($a) === filegroup($b);

echo 'Same group: ', var_export($same, true), PHP_EOL;

unlink($a);
unlink($b);

How It Works

  1. Both files are created by the same PHP process in the same folder.
  2. filegroup() returns the group ID of each file.
  3. The strict comparison === checks that the two numbers are identical.
  4. 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.

Next: fileinode()

fileinode() returns the inode number of a file. An inode is the internal record that a Uni.

fileinode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful