I have a PHP script that is attempting to create a file, but it can not because of permissions. I would like to identify which user the system sees as the one requesting to create the file. I would like to know this in a general way, not just a specific solution on how to allow files to be created by PHP.
Unless the script is changing it's UID, which requires the script to have the SUID bit set in it's permissions, it is running as the user who invoked it. If this is a script run by a web server that would usually be the userid of the web server.
There are various ways for a script to determine what it's uid is. If the scripting language has getuid and geteuid functions available, they can be used to get the real UID and effective UID. If not, running the
id command from the script (either directly, or as shell command will return the UID being used.
File creation permissions are controlled by the directory. The UID needs execute access to all the directories on the path. To create the file, the UID needs write and execute access to the containing directory. To list the created file, the UID needs read and execute access to the directory. Permissions can be gained by user, group, or other permissions.
A last ditch approach would be to change the permissions on the directory to
773 and running the script. If the script is not blocked by permissions of a directory on the path, the file will have the script's UID as the owner. Be sure to restore the original permissions after the test.
I usually use group access to permit the web server to write directories. This is done by setting the group on the directories to the group id the web server (script) runs as.