Finding the Apache user in PHP

In PHP, knowing what the Apache user is on your webserver is very useful. Anything that writes a file to the server for example sessions, uploading files or other temporary file operations, needs to have the destination folder writeable by Apache. If you’re developing locally that’s not usually a problem. But as soon as you’re out in the real world file permission issues can sometimes be a pain.

If you’re having difficulties finding out what the Apache user is try this simple script to help you out.

It basically writes a small temporary file, checks the owner username, gets rid of the file and prints the Apache username to the screen. It should work on PHP4 and PHP5.