tags:

views:

142

answers:

7

I'm writing a simple web app in PHP that needs to have write access to a directory on the server, if the directory isn't writable I want to display an error message explaining to set the owner of the directory to whoever the webserver is being run as (eg. www-data, nobody) but as most people don't know this it would be nice to be able to tell them who to chown the directory to. Is it possible to find this from PHP?

+1  A: 

Check out shell_exec()

karim79
A: 

If you can run some bash, you can use whoami or you might be able to poke around in /proc

BCS
A: 

The quick-and-dirty trick I use is to write a file to /tmp/. It should be created with the user and group used by the PHP process.

Ben Blank
A: 

That error message falls apart in a windows server environment. "Please make sure the web server has write access to the directory" is probably a better error message.

gnarf
+1  A: 

If on linux/unix:

<?php
$temp = tmpfile();
print_r(posix_getpwuid(fileowner($temp)));
?>
Jordan S. Jones
+3  A: 
<?php
  echo exec('whoami');
?>
Ayman Hourieh
A: 

On Unix platforms, this solution might work even with safe mode on, provided that the posix extension is installed or compiled in.

$user = posix_getpwuid(posix_geteuid());
echo $user['name'];

Docs are here.

gnud