lchgrp
-
This function’s solution assumes a Unix-like operating system.
The
lchgrpfunction in PHP will change the group of a file to a different group ID, assuming the PHP process has sufficient privileges to do so. It is the same as PHP’schgrp, but it does not follow the last symbolic link. Instead, it changes the link itself.PHP
lchgrp('/path/to/foobar', 501);
Ruby’s
File.lchownclass method is used to accomplish the same task.Ruby
File.lchown(nil, 501, '/path/to/foobar')
The first argument of
File.lchownis the user, the second is the group, and the third is the file. Above, the user is set tonil. This will leave the user unchanged.Changing the Group by Name
One nice feature of PHP’s
lchgrpis that the$groupargument is mixed. It can accept either a group ID or a group name.PHP
lchown('/path/to/foobar', 'herbert');
File.lchownfrom Ruby is not as flexible. It only accepts numeric IDs. To change the group by name, we must first perform a lookup to get the group ID from the name.The
Etcmodule from the Ruby Standard Library contains a methodgetgrnamthat returns aStructof information about a group by name, which includes the group ID. We can use this lookup the group ID and then pass it toFile.lchown.Ruby
require 'etc' gid = Etc.getgrnam('herbert').gid File.lchown(nil, gid, '/path/to/foobar')
see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

