The chgrp Function

The chgrp function changes the group for the specified file. The first parameter of the function is the file path, the second is the name or ID of the new group. Appropriate permissions are required for the function to execute successfully.

Syntax

phpchgrp(string $filename, string|int $group): bool

Example

Let's change the group of the file 'test.txt' to 'www-data':

php<?php
  $res = chgrp('test.txt', 'www-data');
  var_dump($res);
?>

Code execution result:

phptrue

Example

Let's try to change the group of a non-existent file:

php<?php
  $res = chgrp('nonexistent.txt', 'www-data');
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Using a numeric group ID instead of a name:

php<?php
  $res = chgrp('test.txt', 33); // 33 is a typical ID for the www-data group
  var_dump($res);
?>

Code execution result:

phptrue

See Also

  • the chmod function,
    which changes access permissions
  • the chown function,
    which changes the file owner