Recently, I was working with the file_get_contents() and file_put_contents() functions to read in a file, change its contents, and write it to the file system. Although the is_writable() function returned TRUE, my file_put_contents() command was returning FALSE. This lead me to try a fopen command with the "w" switch (which opens a file for writing and truncates it), and still no luck. Although the file had group permissions to read and write on the file system, the file was owned by a non-apache user. In certain PHP configurations (safe mode, etc) this will not work.
I then created a hook_requirements() function to test for the proper file permissions for my custom module. When I deployed my code to the test server, I got a white screen, which usually signifies a PHP fatal error. Upon review of the apache vhost error log, I saw the function I used "posix_getpwuid" was not defined.
In the end I decided to create a helper function that returns the owner of a file:
<?php
function _MYMODULE_get_file_owner($file) {
// if posix library is installed:
if (function_exists('posix_getpwuid')) {
$owner = fileowner($file);
$ownerInfo = posix_getpwuid($owner);
if ($ownerInfo['name']) return $ownerInfo['name'];
}
// try using ls & awk
$command = "ls -l $file | awk '{print $3}'";
$result = exec($command);
if ($result) return $result;
return false;
}
?>NOTE: the "stat" shell function could also be used:
<?php
$command = "stat -c %U $file";
?>









