lchown
-
This function’s solution assumes a Unix-like operating system.
The
lchownfunction in PHP will change the owner of a file to a different user ID, assuming the PHP process has sufficient privileges to do so. It is the same as PHP’schown, but it does not follow the last symbolic link. Instead, it changes the link itself.PHP
lchown('/path/to/foobar', 501);
Ruby’s
File.lchownclass method is used to accomplish the same task.Ruby
File.lchown(501, nil, '/path/to/foobar')
The first argument of
File.lchownis the user, the second is the group, and the third is the file. Above, the group is set tonil. This will leave the group unchanged.Changing the Owner by User Name
One nice feature of PHP’s
lchownis that the$userargument is mixed. It can accept either a user ID or a user name.PHP
lchown('/path/to/foobar', 'herbert');
File.lchownfrom Ruby is not as flexible. It only accepts numeric IDs. To change the user by name, we must first perform a lookup to get the user ID from the name.The
Etcmodule from the Ruby Standard Library contains a methodgetpwnamthat returns aStructof information about a user by name, which includes the user ID. We can use this lookup the user ID and then pass it toFile.chown.Ruby
require 'etc' uid = Etc.getpwnam('herbert').uid File.lchown(uid, nil, '/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.

