[英]__DIR__ in PHP Drush script is not returning the script path
When adding the line: 添加行时:
#!/usr/bin/env drush
to the top of a PHP script to automatically run using drush, the variable __DIR__
and __FILE__
stop returning my script file name and directory and return drush paths instead. 在使用drush自动运行的PHP脚本的顶部,变量__DIR__
和__FILE__
停止返回我的脚本文件名和目录,而是返回drush路径。
Is there another variable that should be used in case of drush scripts to get the script path and dir? 如果使用drush脚本,是否应该使用另一个变量来获取脚本路径和目录?
Note: When I remove that line and use drush scr <myscript>
instead, __DIR__
and __FILE__
return the correct paths. 注意:当我删除该行并使用drush scr <myscript>
, __DIR__
和__FILE__
返回正确的路径。
[EDIT] [编辑]
To test the suggestions below in the answer by Hasan Bayat: 要测试Hasan Bayat的答案中的以下建议:
I ran the following script: 我运行了以下脚本:
<?php
print('__DIR__ value is: '.__DIR__."\n");
print('__FILE__ value is: '.__FILE__."\n");
print('SCRIPT_FILENAME is: '.$_SERVER['SCRIPT_FILENAME']."\n");
print('PATH_TRANSLATED is: '.$_SERVER['PATH_TRANSLATED']."\n");
print('from backtrace: '.debug_backtrace()[count(debug_backtrace()) - 1]['file']."\n");
print('included_files: '.get_included_files()[0]."\n");
Using the command line: 使用命令行:
drush scr script.php
And the results are as following 结果如下
__DIR__ value is: /path/to/my/script
__FILE__ value is: /path/to/my/script/script.php
SCRIPT_FILENAME is: /path/to/current/dir/index.php
PATH_TRANSLATED is: /usr/local/bin/drush
from backtrace: /usr/local/bin/drush
included_files: /usr/local/bin/drush
I then changed the script to add the line #!/usr/bin/env drush
at the top. 然后,我更改了脚本,在顶部添加了#!/usr/bin/env drush
行。 And reran the script this time with the command: 这次使用命令重新运行脚本:
./script.php
Now the results are as following: 现在结果如下:
__DIR__ value is: phar:///usr/local/bin/drush/commands/core
__FILE__ value is: phar:///usr/local/bin/drush/commands/core/core.drush.inc(1194) : eval()'d code
SCRIPT_FILENAME is: /path/to/current/dir/index.php
PATH_TRANSLATED is: /usr/local/bin/drush
from backtrace: /usr/local/bin/drush
included_files: /usr/local/bin/drush
So apparently none of the suggested solutions works in case the drush first line is added to the script. 因此,如果在脚本中添加了第一行,那么显然所有建议的解决方案都不会起作用。
There are several ways but here is two: 有几种方法,但是有两种:
$_SERVER
Global Variable: Check your $_SERVER["SCRIPT_FILENAME"]
has exists and points to the current file, then if it not works use $_SERVER["PATH_TRANSLATED"]
it might works. 使用$_SERVER
全局变量:检查$_SERVER["SCRIPT_FILENAME"]
是否存在并指向当前文件,然后,如果不起作用,请使用$_SERVER["PATH_TRANSLATED"]
起作用。 $_SERVER
variable not works use the below code: 如果$_SERVER
变量不起作用,请使用以下代码: $stack = debug_backtrace();
$firstFrame = $stack[count($stack) - 1];
$initialFile = $firstFrame['file'];
if none of them works use getcwd();
如果它们都getcwd();
使用getcwd();
to get current working directory and follow it by your file name. 获取当前的工作目录,并在其后加上您的文件名。
EDIT : The third way is to using get_included_files()
, if you don't have any included files previously your current file should be: 编辑 :第三种方法是使用get_included_files()
,如果以前没有任何包含的文件,则当前文件应为:
$included_files = get_included_files();
echo $included_files[0]; // Outputs current script path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.