Re: How to get server user?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Feb 18, 2015, at 7:03 AM, Jason Edgecombe wrote:

On 02/17/2015 01:28 PM, Jeffry Killen wrote:

On Feb 17, 2015, at 7:17 AM, Jason Edgecombe wrote:



I'm saying proceed, then check the result. For example, you might use this code if you want to write to a new file:
|<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
if (file_put_contents($file, $person, LOCK_EX)) {
echo "success\n";
} else {|
||  echo "failed\n";
|}
?>|

If you are replacing the contents of a file, it's usually better to copy/write to a new file, update the file, then rename() the new file to the old file's name. This is the same as 'mv' in Linux/ unix, which makes things a little cleaner. I think that rename/mv is atomic, but I could be wrong.

Jason

This is pretty much what I am doing.
public static function getOwner($_target)
              {
               $_astat = stat($_target);
               $_owner = '';
foreach(posix_getpwuid ( $_astat['uid'] ) as $_sKey=> $_value)
                 {
                  if($_sKey == 'name')
                    {
                     $_owner = $_value;
                    }
                 }
               clearstatcache();
               return $_owner;
             }
And, as you suggest, write a file with code and run it through this to get the owner user name. I am trying to pre screen file related related function calls with this.

I am in the habit of using templates for flat file database files. They produced php code
files like
<?php
$_someVar = someVal;
// add ->
?>
So, if I want to add to this file, I just str_replace the target string '// add ->'
with the new content and another instance of the target string.

So now I have a small php script file that was generated by writing a file and using the above function (geOwner)
<?php
$_serName = "_www";
?>
And all I have to do is include this file wherever I want to find out if a user is the web server user.

The portability issue would be with the posix related functions, I presume.

Thanks again
JK


I don't think that we're on the same page. Why do you need to know the owner of the file and the server? Why are you duplicating the permissions checking of the filesytem?

Jason

It is just to determine if a file is editable by the server via php script and give the user some friendly indication of why it may not be and what could be done about it, instead of the user seeing error messages generated by the script. A la defensive programming.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux