When you do a posix_setuid from root to some other users you will not have access to files owned by root according to their permissions. For instance if you change owner of the process and still need to open a file for read or write with 600 permission owned by root you will receive a permission denied.
There are some ways to do this (i.e. a unix socket or tcp daemon etc), but probably the most easiest way is:
Open the file before changing ownership of process, save the file pointer in a global variable and use it after changing ownership.
For example assume /root/test_file is a file owned by root:root and have a permission of 600 and you are running this script under root. This code will not work:
<?php
posix_setgid(99);
posix_setuid(99);
$fd = fopen('/root/test_file','a');
fwrite($fd,"some test strings");
fclose();
?>
But this one will work:
<?php
$fd = fopen('/root/test_file','a');
posix_setgid(99);
posix_setuid(99);
fwrite($fd,"some test strings");
fclose();
?>
Hope this helps some one.
[Tested on CentOS 5 - Linux 2.6.x - PHP 5.2.x]