I have a php cli script which is executed from /, but the script lies in /opt/script/script.php. How can I get the dynamic location of the script from within the script
$location = ... (Get the location of the script)
echo $location == '/opt/script' ? 'YAY' : 'Stupid';
Output
YAY
That kind of thing.
dirname(__FILE__) (or __DIR__ in newer versions) should be enough. You can find further reference at the Magic Constants chapter.
Choose what you need, you might be looking for __FILE__ (a magic constant containing the filename of the current file) according to your comment:
echo 'Filename (as called): ', var_dump($argv[0]);
echo 'Current working directory (normally the directory called in): ', var_dump(getcwd());
echo 'Path of the script: ', var_dump(__FILE__);
echo 'Directory of the script: ', var_dump(__DIR__);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With