1

I am trying to get content from file at home directory and want to avoid absolute path using tilde in path

I have created file in shell

echo "test" > ~/test_file.txt

Then I am trying to read the file with PHP file_get_contents()

file_get_contents('~/test_file.txt')

Getting following PHP error

Warning Error: file_get_contents(~/test_file.txt): failed to open stream: No such file or directory

Update

This is duplicate of https://stackoverflow.com/a/1894954/2686510

Code snippet I use now

$home = getenv("HOME");
file_get_contents($home . DIRECTORY_SEPARATOR . 'test_file.txt');
Luboš Remplík
  • 526
  • 5
  • 10

1 Answers1

1

Answer for your question is here https://stackoverflow.com/a/1894954/2686510

In short, use $_SERVER['HOME'] variable to access current user home folder.

Nedvajz
  • 891
  • 8
  • 13
  • If you've found an answer in a different question, then this one is a duplicate. In those cases, instead of answering, please flag the question as a duplicate (`Flag` link at the bottom of the post > `a duplicate` > paste the link of the duplicate). – El_Vanja Feb 10 '21 at 10:17
  • Aha, I noticed this way for answering by reference to other thread many times before so did not know it is not valid way to do so .. How to amend ? – Nedvajz Feb 10 '21 at 10:21
  • To be more pedantic :D ... the other question is about CLI only .. but this one is related `file_get_contents()` in general no matter if CLI or not .. booyah .. – Nedvajz Feb 10 '21 at 10:24
  • Referencing other answers is perfectly fine if that's a part of your answer (if you're adding something else that isn't solved in the other answer, like if you've used someone else's function within your algorithm). Here, you've basically said "look over there". That's just duplicate content. – El_Vanja Feb 10 '21 at 10:28