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
$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
- file_put_contents() creates a small demo file.
- filegroup() reads the current group ID of the file.
- chgrp() sets the group to that same ID, which is always allowed.
- unlink() removes the demo file when we are done.

