Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP chgrp function

Today we will see what the PHP function chgrp does. In this post, we will deviate a little from what is given on php.net. Why? This is because PHP, in many of its naming conventions, draws from those of Linux, and I believe that a better explanation is to talk about both.

Introduction

Long ago, in Linux, chown and chgrp were used to change the owner and group, in fact, chown stands for “change owner” and chgrp stands for “change group”.

Nowadays, in Linux, we can do this using chown alone if we want to. For example, in WordPress:

sudo chown -R www-data:www-data wp-content

www-data is the owner and www-data is also the group, with “:” indicating the group.

If our command were:

sudo chown -R www-data wp-content

We would be changing only the owner.

We cannot do this directly with PHP, as we don’t know how the chgrp function works internally.

Therefore, in PHP, we will have to use chown and chgrp, and we should not use www-data:www-data within chown.

The chgrp function

You should not use this function on non-UNIX systems.

It is important to note that in order to use the chgrp function, you must have the appropriate permissions on the file system to make changes to the groups.

It changes the group of the file; it allows assigning a new group to the specified file or directory.

Description / Descripción
chgrp(string $filename, mixed $group): bool

Parameters

$filename – It is the path of the file or directory where the group change will be performed.

$group – It can be either a group name (as a string) or a group identification number (as an integer).

Returns

(boolean) true In case of success (the function ran without errors and was able to make the change),

(boolean) false case of error.

Ejemplo simple
$filename = '/ruta/al/archivo.txt';
$group = 'nuevogrupo';

if (chgrp($filename, $group)) {
    echo "El grupo del archivo se ha cambiado correctamente.";
} else {
    echo "No se pudo cambiar el grupo del archivo.";
}
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category