2

I searched for this information on Google but couldn't find anything detailed. I was reading about the chgrp() and it mentioned that this function can change file group.

There is no explanation about what a file group is. I also tried to run the example on reference page to see if the output could help me understand more about the function but the output seemed to be just the number zero. Here is the example code:

$filename = 'stats.txt';
$format = "%s's Group ID @ : %d\n";
printf($format, $filename, filegroup($filename));
chgrp($filename, 10);
clearstatcache(); // do not cache filegroup() results
printf($format, $filename, filegroup($filename));

Here is the output of that code:

stats.txt's Group ID : 0
stats.txt's Group ID : 0
Phil
  • 157,677
  • 23
  • 242
  • 245
Vineet Sharma
  • 221
  • 2
  • 11
  • @MichaelBerkowski So, this function is only useful for Unix filesystem? – Vineet Sharma Aug 21 '17 at 03:26
  • 2
    I cannot find the place in official PHP documentation where it explicitly says so, but as far as I understand, `chmod(),chown(),chgrp()` are only available or applicable when PHP is running under a unix-like system (Linux, MacOS) and not applicable in Windows. – Michael Berkowski Aug 21 '17 at 03:29
  • this might help to find fitting information: `man chgrp`; likely chgrp works the same way as chmod on windows: https://stackoverflow.com/questions/739891/php-mkdir-chmod-and-windows – no dont expect the same result as on *nix systems – vv01f Aug 21 '17 at 03:29
  • Is this a question about php running under windows? If so, you should update the tags on the question. I also would have to ask, why you would be running php/webstack under windows? – gview Aug 21 '17 at 03:44
  • 2
    The downvote on this question is extremely harsh. It seems OP was reading the PHP manual (awesome!), found something they were unfamiliar with and experimented (awesome!) but the results were confusing. This has all the hallmarks of a great StackOverflow question. – Phil Aug 21 '17 at 03:52
  • @Phil great feedback. I agree and apparently so did the down voter. – gview Aug 21 '17 at 03:54

1 Answers1

2

In unix, file ownership is based on an owner and a group.

Here's a directory listing on a linux box:

[vagrant@localhost:nginx]$ ls -l
total 8
drwxrwxrwx. 1 vagrant vagrant  136 Feb  5  2016 configs
-rwxrwxrwx. 1 vagrant vagrant 1348 Aug 19 07:34 docker-entrypoint.sh
-rwxrwxrwx. 1 vagrant vagrant  786 Feb  5  2016 Dockerfile

In this case, the file docker-entrypoint.sh is owned by a user named "vagrant" and a group named "vagrant".

Permissions are then assigned to one of 3 separate groups:

  • Owner permissions
  • Group permissions
  • Other users

You are focused on the chgrp() function which is simply a wrapper around the linux chgrp api/command.

The groups on a linux box can be examined with

cat /etc/group

The number at the end of each entry is the group number. You can typically manipulate group membership using a group name or number, so in your example, you are displaying a group number apparently, but there is some group name associated with that group.

[vagrant@localhost:nginx]$ cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:

There are similarities and differences between windows and unix/mac systems but they aren't 100% equivalent. Here's a good article on this topic.

gview
  • 14,876
  • 3
  • 46
  • 51