0

I've been looking for a way to get the current working directory without using getcwd. The reason being that the return from this function is mutable by chdir. Additionally because of this I suspect that getcwd is a blocking function.

I don't mind if the solution is blocking, just so long as it is immutable (and thusly cachable) and callable from anywhere (i.e. no bootstrapping). It needs to always be the directory from which the process was executed.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • `__DIR__` is a const so it should be immutable (don't quote me on that :P), and might fit what you need? It returns the location of the file in use. You can read more about the difference here https://stackoverflow.com/questions/2184810/difference-between-getcwd-and-dirname-file-which-should-i-use – Brett Gregson Aug 20 '19 at 12:44
  • Thanks but no thats not what I'm after. Suppose you have your script in `/path/to/script.php`, running it from `/path` like `./to/script.php` will mean the CWD is still `/path`. My problem is that `chdir` will change that (not really, its a simulation) but the original canonical CWD goes down the sink, I can't find anyway to get it back. – Flosculus Aug 20 '19 at 12:47

1 Answers1

0

Found it. I didn't realize $_SERVER had so much information on CLI.

var_dump($_SERVER['PWD']);

Still not entirely immutable, but very bad practice to alter which is good enough.

Flosculus
  • 6,880
  • 3
  • 18
  • 42