User home directory php

Содержание
  1. Referencing the home directory in CLI PHP
  2. 3 Answers 3
  3. PHP. Get user home directory (for virtual hosting)
  4. 3 Answers 3
  5. How to find user’s home directory on Windows and Linux in PHP
  6. Solution:
  7. Share solution ↓
  8. Additional Information:
  9. Didn’t find the answer?
  10. Similar questions
  11. Write quick answer
  12. About the technologies asked in this question
  13. PHP
  14. Welcome to programmierfrage.com
  15. Get answers to specific questions
  16. Help Others Solve Their Issues
  17. Latest questions:
  18. 273 php — Need Help to Set cookie to remember button click and disable it for X minutes
  19. 476 php — Fatal error: Call to undefined function validation_errors() using codeIgniter
  20. 481 php — How to copy the tables of an existing MySQL database to a new database without the records?
  21. 525 PHP — Would i need to create 2 separate objects to hold database data from 2 separate tables?
  22. 869 Saving the WordPress header to a php file accessible anywhere?
  23. 51 php — Reading an Ical(.ics) file gives me wrong times. What am i missing?
  24. 696 session — php ignores if for $_SESSION[‘variable’]
  25. 941 php — Laravel 4 store() method error MethodNotAllowedHttpException
  26. 136 java — Php, clone objects, add new methods, or overwrite them
  27. 852 html — PHP Image upload with iOS Safari not providing TMP_NAME
  28. Users questions:
  29. mysql
  30. Warning: Undefined array key «program» in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key «enter_course» in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key «enter_address» in E:\xampp\htdocs\pi\alumni.php on line 32
  31. Rename existing images with keyword in WordPress PHP
  32. codeigniter
  33. Class «web_profiler.controller.profiler» does not exist after upgrading symfony 3.4 to 4.4 version
  34. How to get the home directory from a PHP CLI script
  35. Join the world’s most active Tech Community!
  36. Welcome back to the World’s most active Tech Community!
  37. Subscribe to our Newsletter, and get personalized recommendations.
  38. TRENDING CERTIFICATION COURSES
  39. TRENDING MASTERS COURSES
  40. COMPANY
  41. WORK WITH US
  42. DOWNLOAD APP
  43. CATEGORIES
  44. CATEGORIES
Читайте также:  Airflow python sensor example

Referencing the home directory in CLI PHP

‘$HOME/Documents/someFile.txt’ — variables don’t get parsed in single quotes, the $HOME ‘s value is unknown.

3 Answers 3

The expansion of ~ and $HOME is done by your shell, not by PHP. Depending on your config, you can probably use:

$data = file_get_contents($_SERVER['HOME'] . '/Documents/someFile.txt'); 

Use getenv(‘HOME’) or $_SERVER[‘HOME’] . You cannot use $HOME in single quotes because its literal will be used, and ~ isn’t known by php.

Thanks. that works. I was purposefully trying to use it in single quote as its not a PHP variable. I thought it would work as string in command line. Clearly, I need more research into using php in the cli.

$data = file_get_contents('$HOME/Documents/someFile.txt'); 
  1. You never set the variable $HOME . HOME is an environment variable, but they don’t automatically become PHP variables like they do in the shell.
  2. Variables aren’t expanded inside single quotes, see What is the difference between single-quoted and double-quoted strings in PHP? (not coincidentally, this is the same as the shell).
$HOME = getenv('HOME'); $data = file_get_contents("$HOME/Documents/someFile.txt"); 

Yes, i’m aware of the differences between single/double quotes in both php and bash. I didn’t however realize I had to pass the $HOME environment variable to PHP. My thought was to simply pass the path to file_get_contents as string as I would normally navigate in cli.

Источник

PHP. Get user home directory (for virtual hosting)

It’s working correctly on my local server, on Amazon instances. But it outputs only «~» on virtual hosting. Maybe, do you have working solution for getting home directory of current user? Thanks in advance

Was this question about Linux or. in general? I ask that since in some comments I’ve seen some people commenting about Microsoft Windows, and they probably would like to answer about it without downvotes. Thanks for this clarification

Читайте также:  Java class file description

3 Answers 3

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’] .

Also note that you may be interested in posix_geteuid() (effective UID) instead of posix_getuid() (real UID) — php.net/manual/en/function.posix-geteuid.php

Use $_SERVER[‘HOME’] or you can try with

Note: This requires that you execute the script via the command-line.

On most regular servers, those daemons (apache, nginx, etc.) have no real «home directory».

If you consider «virtual hosting», that wouldn’t be possible, as there can only be one home directory per user, but many vhosts per daemon.

I guess what you are looking for, ist the environment variable DOCUMENT_ROOT (the root directory for the current vhost documents).

Note that the original questioner is running shell commands with shell_exec so I really think the user is interested in having the Unix/Linux HOME, to run some commands using that, and the user is probably not interested in the document root. But I agree this is still useful to clarify

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Источник

How to find user’s home directory on Windows and Linux in PHP

Solution:

After not working on this for a long time, I finally decided to definitely answer this question.

There are some usefull environment variables defined on Windows: USERPROFILE , APPDATA , LOCALAPPDATA . They are easily accessible via getenv() function:

getenv('USERPROFILE'); 

So, on Windows, it seems to be reliable.

If you need to store data for the current user, APPDATA and LOCALAPPDATA are good variables to find that place.

I’ve written a package to make these tools reusable: https://github.com/Arcesilas/Platform

It’s still work in progress and certainly needs to be improved. Any help is welcome to make this tool reliable on any platform.

Thanks to eryksun whose comments helped a lot in solving this question.

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Latest questions:

476 php — Fatal error: Call to undefined function validation_errors() using codeIgniter

481 php — How to copy the tables of an existing MySQL database to a new database without the records?

525 PHP — Would i need to create 2 separate objects to hold database data from 2 separate tables?

869 Saving the WordPress header to a php file accessible anywhere?

51 php — Reading an Ical(.ics) file gives me wrong times. What am i missing?

696 session — php ignores if for $_SESSION[‘variable’]

941 php — Laravel 4 store() method error MethodNotAllowedHttpException

136 java — Php, clone objects, add new methods, or overwrite them

852 html — PHP Image upload with iOS Safari not providing TMP_NAME

Users questions:

mysql

Warning: Undefined array key «program» in E:\xampp\htdocs\pi\alumni.php on line 27 Warning: Undefined array key «enter_course» in E:\xampp\htdocs\pi\alumni.php on line 31 Warning: Undefined array key «enter_address» in E:\xampp\htdocs\pi\alumni.php on line 32

Rename existing images with keyword in WordPress PHP

codeigniter

Class «web_profiler.controller.profiler» does not exist after upgrading symfony 3.4 to 4.4 version

© 2021-2023 Programming problem solving site for beginners and advanced. Answers to questions related to coding.

This site uses cookies. We use them to improve the performance of our website and your interaction with it. Confirm your consent by clicking OK

Источник

How to get the home directory from a PHP CLI script

  • All categories
  • ChatGPT (11)
  • Apache Kafka (84)
  • Apache Spark (596)
  • Azure (145)
  • Big Data Hadoop (1,907)
  • Blockchain (1,673)
  • C# (141)
  • C++ (271)
  • Career Counselling (1,060)
  • Cloud Computing (3,469)
  • Cyber Security & Ethical Hacking (162)
  • Data Analytics (1,266)
  • Database (855)
  • Data Science (76)
  • DevOps & Agile (3,608)
  • Digital Marketing (111)
  • Events & Trending Topics (28)
  • IoT (Internet of Things) (387)
  • Java (1,247)
  • Kotlin (8)
  • Linux Administration (389)
  • Machine Learning (337)
  • MicroStrategy (6)
  • PMP (423)
  • Power BI (516)
  • Python (3,193)
  • RPA (650)
  • SalesForce (92)
  • Selenium (1,569)
  • Software Testing (56)
  • Tableau (608)
  • Talend (73)
  • TypeSript (124)
  • Web Development (3,002)
  • Ask us Anything! (66)
  • Others (2,231)
  • Mobile Development (395)
  • UI UX Design (24)

Join the world’s most active Tech Community!

Welcome back to the World’s most active Tech Community!

Subscribe to our Newsletter, and get personalized recommendations.

GoogleSign up with Google facebookSignup with Facebook

Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

  • DevOps Certification Training
  • AWS Architect Certification Training
  • Big Data Hadoop Certification Training
  • Tableau Training & Certification
  • Python Certification Training for Data Science
  • Selenium Certification Training
  • PMP® Certification Exam Training
  • Robotic Process Automation Training using UiPath
  • Apache Spark and Scala Certification Training
  • Microsoft Power BI Training
  • Online Java Course and Training
  • Python Certification Course
  • Data Scientist Masters Program
  • DevOps Engineer Masters Program
  • Cloud Architect Masters Program
  • Big Data Architect Masters Program
  • Machine Learning Engineer Masters Program
  • Full Stack Web Developer Masters Program
  • Business Intelligence Masters Program
  • Data Analyst Masters Program
  • Test Automation Engineer Masters Program
  • Post-Graduate Program in Artificial Intelligence & Machine Learning
  • Post-Graduate Program in Big Data Engineering

COMPANY

WORK WITH US

DOWNLOAD APP

appleplaystore googleplaystore

CATEGORIES

CATEGORIES

  • Cloud Computing
  • DevOps
  • Big Data
  • Data Science
  • BI and Visualization
  • Programming & Frameworks
  • Software Testing © 2023 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved. Terms & ConditionsLegal & Privacy

Источник

Оцените статью