开发者

PHP CLI: what directory?

开发者 https://www.devze.com 2023-04-10 06:49 出处:网络
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

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 locati开发者_开发问答on 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__);
0

精彩评论

暂无评论...
验证码 换一张
取 消