How to get the home directory from a PHP CLI script?
You can fetch the value of $HOME from the environment:
<?php
$home = getenv("HOME");
?>
Use $_SERVER['HOME']
Edit:
To make it complete, see what print_r($_SERVER)
gave me, executed from the command line:
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 don't expose relevant security information ;)
Windows Compatibility
Note that $_SERVER['HOME']
is not available on Windows. Instead, the variable is split into $_SERVER['HOMEDRIVE']
and $_SERVER['HOMEPATH']
.