PHP chgrp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, chgrp() changes the group that owns a file or folder. You can give the group as a name like www-data or as a number (the group ID).

Only the super user can change a file to any group. A normal user can usually change it only to a group they belong to. The function does not work on Windows, where it does nothing and returns true.

Syntax

Call chgrp() with the values you need. Start with this line.

PHP
$ok = chgrp(__DIR__ . "/demo.txt", "www-data");
CallWhat it does
chgrp($file, "www-data")Changes the group by name.
chgrp($file, 33)Changes the group by its numeric ID.
chgrp($file, filegroup($file))Sets the same group again, which is a safe way to test the call.

chgrp() clears the file status cache for the file after changing it. On Windows it silently returns true without changing anything.

Library declarations
PHP
chgrp(string $filename, string|int $group): bool

Parameters

ParameterDescription
filenameRequired. The path to the file or folder.
groupRequired. The group name as a string, or the group ID as an integer.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Change the Group of a File

Create a file, read its current group, and set it again with chgrp(). The group ID in your output will be different.

PHP
<?php
$file = __DIR__ . "/demo.txt";
file_put_contents($file, "Hello");

$gid = filegroup($file);

if (chgrp($file, $gid)) {
    echo "Group changed to ID ", $gid, PHP_EOL;
}

unlink($file);

How It Works

  1. file_put_contents() creates a small demo file.
  2. filegroup() reads the current group ID of the file.
  3. chgrp() sets the group to that same ID, which is always allowed.
  4. unlink() removes the demo file when we are done.

Example 2 — Handle a Missing File

chgrp() returns false and raises a warning if the file does not exist. Use the @ operator or check first.

PHP
<?php
$file = __DIR__ . "/missing.txt";

$result = @chgrp($file, "www-data");

var_dump($result);

if (!$result) {
    echo "Could not change the group.", PHP_EOL;
}

How It Works

  1. The path points to a file that was never created.
  2. The @ operator hides the warning that PHP would normally print.
  3. chgrp() returns false because the file is not there.
  4. We check the result and print a friendly message.

Example 3 — Change the Group of Several Files

Apply the same group to a list of files and count how many were changed.

PHP
<?php
$files = [__DIR__ . "/a.txt", __DIR__ . "/b.txt"];
$changed = 0;

foreach ($files as $file) {
    file_put_contents($file, "data");

    if (chgrp($file, filegroup($file))) {
        $changed++;
    }
}

echo "Changed {$changed} files", PHP_EOL;

array_map("unlink", $files);

How It Works

  1. We create two demo files in the script folder.
  2. chgrp() is called for each file with its own group ID.
  3. Each success adds one to the $changed counter.
  4. array_map() with unlink cleans up both demo files.

Frequently Asked Questions

It changes the group ownership of a file or folder. The group can be a name like www-data or a numeric group ID.
The most common reasons are that the file does not exist or the PHP user is not allowed to change the group. Normal users can only use groups they belong to.
No. On Windows it does nothing and always returns true, so do not rely on it for real changes there.
chgrp() changes the group that owns the file, while chown() changes the user that owns it. chmod() is different again because it changes the permissions.
Use filegroup($file). It returns the numeric group ID, and posix_getgrgid() can turn that ID into a group name.

Next: chmod()

chmod() changes the permissions (the read, write, and execute rights) of a file or folder.

chmod() 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