I have a file system structure with symlinks, something like this:
folder123
index.php
config.php -> symlink to shared/config.php
shared
config.php
I run index.php on command line, index.php includes config.php, which is symlink to a file in some other folder.
How can I know index.php file path from the config.php script? The required result is “folder123” (well the full path of it), not “shared”. So, I cannot use __FILE__.
This works on scripts, ran by browser (at least in my case, it is the same as folder123 – the base dir):
$_SERVER['DOCUMENT_ROOT']
I need something for the command-line too.
It looks like
$_SERVER['PWD']
is something what I need, but it’s not documented, so I don’t find it a reliable way. Also, you need to combile it with SCRIPT_FILENAME to get the required results – PWD shows directory, from which the script was run, not it’s full path.
getcwd looks promising, but it can be changed by chdir – I’ll use that if I don’t find some better alternative.
I’m not sure if there is a way to determine what file included a file, but you can get the name of the running program (
index.phpin this case) with$argv[0], or possibly$_SERVER['argv'][0]if the former is not available.