How to get home directory from PHP CLI script?

From the command line, I can get the home directory as follows:

~/ 

How can I get the home directory inside my PHP CLI script?

 #!/usr/bin/php <?php echo realpath(~/); ?> 
+52
php
Dec 12 '09 at 22:33
source share
10 answers

Use $_SERVER['HOME']




Edit:

To do this, look at what print_r($_SERVER) from the command line gave me:

 Array ( [TERM_PROGRAM] => Apple_Terminal [TERM] => xterm-color [SHELL] => /bin/bash [TMPDIR] => /var/folders/Lb/LbowO2ALEX4JTK2MXxLGd++++TI/-Tmp-/ [TERM_PROGRAM_VERSION] => 272 [USER] => felix [COMMAND_MODE] => unix2003 [__CF_USER_TEXT_ENCODING] => 0x1F5:0:0 [PATH] =>/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin [PWD] => /Users/felix/Desktop [LANG] => de_DE.UTF-8 [SHLVL] => 1 [HOME] => /Users/felix [LOGNAME] => felix [DISPLAY] => /tmp/launch-XIM6c8/:0 [_] => ./test_php2 [OLDPWD] => /Users/felix [PHP_SELF] => ./test_php2 [SCRIPT_NAME] => ./test_php2 [SCRIPT_FILENAME] => ./test_php2 [PATH_TRANSLATED] => ./test_php2 [DOCUMENT_ROOT] => [REQUEST_TIME] => 1260658268 [argv] => Array ( [0] => ./test_php2 ) [argc] => 1 ) 

I hope I will not disclose relevant security information;)

Windows compatibility

Please note that $_SERVER['HOME'] not available on Windows. Instead, the variable is split into $_SERVER['HOMEDRIVE'] and $_SERVER['HOMEPATH'] .

+65
Dec 12 '09 at 22:46
source share
— -

You can get the value of $ HOME from the environment:

 <?php $home = getenv("HOME"); ?> 
+53
Aug 04 2018-11-18T00:
source share

This feature is taken from the Drush project.

 /** * Return the user home directory. */ function drush_server_home() { // Cannot use $_SERVER superglobal since that empty during UnitUnishTestCase // getenv('HOME') isn't set on Windows and generates a Notice. $home = getenv('HOME'); if (!empty($home)) { // home should never end with a trailing slash. $home = rtrim($home, '/'); } elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { // home on windows $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; // If HOMEPATH is a root directory the path can end with a slash. Make sure // that does not happen. $home = rtrim($home, '\\/'); } return empty($home) ? NULL : $home; } 
+6
Sep 11 '15 at 16:46
source share

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

+5
Jun 18 '15 at 10:22
source share

If for some reason getenv('HOME') does not work or Windows is used as the server OS, you can use exec("echo ~") or exec("echo %userprofile%") to get the user directory. Of course, the exec function must be available (some hosting companies disable these functions for security reasons, but this is unlikely to happen).

Here is a php function that will try to use $_SERVER , getenv and finally check if the exec function exists and use the appropriate system command to get the user directory:

 function homeDir() { if(isset($_SERVER['HOME'])) { $result = $_SERVER['HOME']; } else { $result = getenv("HOME"); } if(empty($result) && function_exists('exec')) { if(strncasecmp(PHP_OS, 'WIN', 3) === 0) { $result = exec("echo %userprofile%"); } else { $result = exec("echo ~"); } } return $result; } 
+2
Jan 21 '18 at 14:14
source share

Depends on where you are and what you are trying to do.

$_SERVER completely unreliable for a non-server script, BUT $_ENV['HOME'] might be better for a standard shell script. You can always print_r( $GLOBALS ) and see what your environment gives you the opportunity to play with. phpinfo() also splashes out plaintxt when called from the CLI script, and only good ole php -i made from the shell will do the same.

+1
Jul 26 2018-11-18T00:
source share

This is the method I used in a recent project:

 exec('echo $HOME') 

I saved it to my object by doing the following:

 $this->write_Path = exec('echo $HOME'); 

But you can really save it the way you want, and use it anyway.

0
Mar 08 '18 at 17:40
source share

$ _ SERVER ['HOME'] and getenv ('home') do not work for me.

However, on PHP 5.5+, this worked to give the home directory that the file is located in:

 explode( '/', __FILE__ )[2] 
-6
Mar 20 '15 at 18:17
source share

Try $_SERVER['DOCUMENT_ROOT'] as your home directory.

-10
Nov 29 '12 at 18:39
source share
 dirname(__DIR__); 
-fourteen
Dec 12 '09 at 22:42
source share



All Articles