fileowner
-
This function’s solution assumes a Unix-like operating system.
The
fileownerfunction in PHP returns the user ID owning a file.PHP
$uid = fileowner('/path/to/foobar'); print $uid; //=> 501
You can retrieve the file owner in Ruby by calling
File.stat. It will return aFile::Statinstance that contains auidattribute.Ruby
stat = File.stat('/path/to/foobar') p stat.uid #=> 501
Resolving the User Name
If you also need the name from the user ID, you can use PHP’s
posix_getpwuidfunction to retrieve this information and more.PHP
$uid = fileowner('/path/to/foobar'); $info = posix_getpwuid($uid); print $info['name']; //=> "herbert"
The
Etcmodule from the Ruby Standard Library provides agetpwuidmethod. It returns aStructwith attributes similar to the keys of the associative array returned by PHP’sposix_getpwuid.Ruby
require 'etc' uid = File.stat('/path/to/foobar').uid info = Etc.getpwuid(uid) p info.name #=> "herbert"
Comparing to the Current Process
Ruby’s
Fileclass provides an additional method,owned?, that tests if the effective user ID of the current process is the user ID of a file.Ruby
File.owned?('/path/to/foobar') #=> true
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.

