Hướng dẫn php get home directory
From the command line, I can get the home directory like this:
How can I get the home directory inside my PHP CLI script?
asked Dec 12, 2009 at 22:33
Use Edit: To make it complete, see what
I hope I don't expose relevant security information ;) Windows CompatibilityNote that
answered Dec 12, 2009 at 22:46
Felix KlingFelix Kling 769k171 gold badges1068 silver badges1114 bronze badges 8 You can fetch the value of $HOME from the environment:
answered Aug 4, 2011 at 18:54
EJ CampbellEJ Campbell 6555 silver badges4 bronze badges 2 PHP allows you to get the home dir of any of the OS users. There are 2 ways. Method #1: First of all you gotta figure out the OS User ID and store it somewhere ( database or a config file for instance).
This code bit can be ran by any OS user, even the usual webserver www-data user, as long as you pass the correct target user ID previously collected.
Documentation Method #2: Same logic from posix_getpwuid(). Here you gotta pass the target OS username instead of their uid.
Documentation
answered Jun 18, 2015 at 22:22
Francisco LuzFrancisco Luz 2,5732 gold badges24 silver badges35 bronze badges 6 This function is taken from the Drush project.
answered Sep 11, 2015 at 16:46
ya.teckya.teck 1,80227 silver badges32 bronze badges If for any reason Here is a
php function that will try
answered Jan 21, 2018 at 14:14
Christos LytrasChristos Lytras 34.7k4 gold badges70 silver badges103 bronze badges 7 Depends on where you are and what you're trying to do.
Tim Cooper 153k37 gold badges320 silver badges272 bronze badges answered Jul 26, 2011 at 18:42
This works for me both LINUX and WINDOWS:
answered Nov 12, 2020 at 19:39
1 This is the method i used in a recent project:
I saved it into my object by doing this:
But you could really save it any way you want and use it anyway you see fit. answered Mar 8, 2018 at 17:40
dustbusterdustbuster 71.9k7 gold badges19 silver badges38 bronze badges 1 You can rely on "home" directory when working with web server. Working CLI it will give the user path (in windows) if you want your script to work in web and cli environments I've found better to go up any levels with dirname until your root(home) directory.
answered Jul 4, 2020 at 14:43
lisandrolisandro 3862 silver badges10 bronze badges 1 I know that this is an old question, but if you are looking for an alternative and easy to replicate method across your application, you may consider using a library installed via composer. I've written a library that combine some of the answer here and make it a library and I'll shamelessly promote it here : juliardi/homedir. You can install it via composer :
And then use it in your application :
Hope this answer help you. answered Nov 24, 2020 at 7:28
$_SERVER['HOME'] and getenv('home') did not work for me. However, on PHP 5.5+, this worked to give the home directory that the file is located in:
answered Mar 20, 2015 at 18:17
iateadonutiateadonut 1,66416 silver badges27 bronze badges 1 Try
phant0m 16.3k5 gold badges47 silver badges81 bronze badges answered Nov 29, 2012 at 18:39
2
answered Dec 12, 2009 at 22:42
streetparadestreetparade 31.1k36 gold badges100 silver badges123 bronze badges 1 |