PHP Get user home directory (for shared hosting)

I want to get the home directory of the current script user (nginx / www / apache, etc.) in PHP. I use

$output_message = shell_exec('echo ~');
var_dump($output_message);

It works correctly on my local server, on Amazon instances. But on shared hosting, it only displays "~".

Perhaps you have a working solution to get the current user's home directory?

Thank you in advance

+4
source share
3 answers

I based a working solution:

$user = posix_getpwuid(posix_getuid())

Returns an array e.g.

Array
(
    [name] => username
    [passwd] => ********
    [uid] => 501
    [gid] => 20
    [gecos] => Full Name
    [dir] => /home/username
    [shell] => /bin/bash
)

So, to access the user's home directory, he $user['dir'].

+15
source

Use $_SERVER['HOME']or try with

$home = getenv("HOME");

. script .

+1

On most ordinary servers, these daemons (apache, nginx, etc.) do not have a real "home directory".

If you consider "shared hosting", this would be impossible, since there can only be one home directory per user, but many vhosts per daemon.

I assume that you are looking for the ist environment variable DOCUMENT_ROOT (the root directory for current vhost documents).

$_SERVER['DOCUMENT_ROOT']
0
source

All Articles