I was wondering if there is some variable that will return what $_SERVER[‘DOCUMENT_ROOT’] returns when I call PHP like this: ./somescript
If not, how do people get around this case? I’m looking to be able to call the same script (a template compiler) in both the web browser and the terminal.
Thanks!
Matt Mueller
I don’t recommend the
getcwd()command for this because you cannot be sure of wherecwdis pointing unless you’ve made achdirbefore (which mean you already know which directory you’re in). Working directory can be define in php config and vary between apache and CLI, and some php frameworks changes it on startup.Use
dirname(__FILE__)it always works wether you’re in a apache or cli context (plus it works on windows and unix), and if you need to move inside your project files you can just use relative paths.