How to get the home directory from a PHP CLI script?
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']
.
How to find path of user's home directory in PHP Cli on linux?
You can find the current username by using whomai
as you already wrote. Use that name to find the line corresponding to that user in /etc/passwd
. From that line cut
out the home dir information:
$currentUserHomeDir = exec('grep `whoami` /etc/passwd | cut -d ":" -f6');
PHP. Get user home directory (for virtual hosting)
I founded working solution:
$user = posix_getpwuid(posix_getuid())
This returns the array, e.g.
Array
(
[name] => username
[passwd] => ********
[uid] => 501
[gid] => 20
[gecos] => Full Name
[dir] => /home/username
[shell] => /bin/bash
)
So to access user home dir, it's $user['dir']
.
How to get document root path in PHP with CLI?
echo($_SERVER["PWD"]); its working
How to get directory where PHP script was called from
Almost hit the "Post question" and guessed to check command line parameters. :-)
$ php -h
...
-C Do not chdir to the script's directory
...
How to use tilde with `file_get_contents` to read file from home directory
Answer for your question is here https://stackoverflow.com/a/1894954/2686510
In short, use $_SERVER['HOME']
variable to access current user home folder.
Get absolute path of initially run script
echo realpath(dirname(__FILE__));
If you place this in an included file, it prints the path to this include. To get the path of the parent script, replace __FILE__
with $_SERVER['PHP_SELF']
. But be aware that PHP_SELF is a security risk!
Get Root Directory Path of a PHP project
For PHP >= 5.3.0 try
PHP magic constants.
__DIR__
And make your path relative.
For PHP < 5.3.0 try
dirname(__FILE__)
$_SERVER document root in CLI
$_SERVER contains headers which won't be available in the CLI. The web server defines the document root. In the CLI, you aren't using a web server, so there is no document root.
You can try to rely on environmental variables, assuming they are set by your shell.
For instance, PWD
represents the current directory and HOME
represents the user's home directory.
$pwd = getenv('PWD');
$home = getenv('HOME');
You can also use __FILE__
or __DIR__
magic constants to try and depict the path you are currently in.
Related Topics
Check If Links Are Broken in PHP
Function to Create Regex Matching a Number Range
How to Decode Numeric HTML Entities in PHP
Cannot Figure Out How to Run a MySQLi_Multi_Query and Use the Results from the Last Query
Userland Multipart/Form-Data Handler
Does Perl Have PHP-Like Dynamic Variables
How to Use Curl to Fetch Specific Data from a Website and Then Save It My Database Using PHP
Skip Checkout in Magento for a Downloadable Product
Query MySQL and Export Data as CSV in PHP
Can't Insert Russian Text into MySQL Database
How to Cause a Redirect to Occur Before PHP Script Finishes
Replace Add to Cart Button with a Read More Linked to Product Page on Shop Pages in Woocommerce 3
Recreate Original PHP Array from Print_R Output
Which Is the Best Character Encoding for Japanese Language for Db, PHP, and HTML Display
How to Use Composer to Autoload Classes from Outside the Vendor