PHP allows you to get the home directory of any of the OS users. There are 2 ways.
Method number 1:
First of all, you need to find out the OS user ID and save it somewhere (for example, a database or configuration file).
// Obviously this gotta be ran by the user which the home dir // folder is needed. $uid = posix_getuid();
This bit of code can be triggered by any OS user, even a regular user of the www-data web server, provided that you pass the correct identifier of the target user previously collected.
$shell_user = posix_getpwuid($uid); print_r($shell_user); // will show an array and key 'dir' is the home dir // not owner of running script process but script file owner $home_dir = posix_getpwuid(getmyuid())['dir']; var_dump($home_dir);
Documentation
Method number 2:
The same logic from posix_getpwuid (). Here you must pass the username of the target OS instead of their uid.
$shell_user = posix_getpwnam('johndoe'); print_r($shell_user); // will show an array and key 'dir' is the home dir // not owner of running script process but script file owner $home_dir = posix_getpwnam(get_current_user())['dir']; var_dump($home_dir);
Documentation
Francisco Luz Jun 18 '15 at 10:22 2015-06-18 22:22
source share