如何在没有shell访问的情况下获取php解释器/二进制文件的完整路径

2022-01-02 00:00:00 path interpreter php

如何从 php 脚本中获取 php 解释器的完整路径(无命令行访问权限).

我需要做的是:

$foo = "/usr/bin/php";回声 $foo;

但我需要先获取路径,以便将其分配给 foo.

如果您有一个在 Windows 和 nix 上都可以运行的更好的解决方案,但如果没有,nix 会很好.

在你问之前,

  1. 问主人是不可能的
  2. 没有外壳访问

问题是使用它输出的任何东西都不起作用.例如 PHP_BINDIR 将输出/usr/bin 但使用/usr/bin/php 无济于事.完整代码为:

exec("php-cli $path_to_file >/dev/null 2>/dev/null &");

但即使使用/usr/bin/php-cli 也不起作用,即使它告诉我.我必须使用:

exec("/opt/php52/bin/php-cli $path_to_file >/dev/null 2>/dev/null &");

例如对于这个特定的主机.

解决方案

你可以用这个常量找到 PHP 二进制路径:

PHP_BINDIR

从 PHP 5.4 开始,您可以使用此常量获取当前实际运行的可执行文件的路径:

PHP_BINARY

http://php.net/manual/en/reserved.constants.php

How can I get the full path to php interpreter from a php script (no command line access).

What I need to do is:

$foo = "/usr/bin/php";
echo $foo;

But I need to get the path first so I can assign it to foo.

If you have a solution that works on both Windows and nix even better but if not, nix would be fine.

Before you ask,

  1. Asking the host is out of the question
  2. No shell access

The problem is that using whatever it outputs doesn't work. For example PHP_BINDIR will output /usr/bin but using /usr/bin/php won't help. The full code is:

exec("php-cli $path_to_file > /dev/null 2>/dev/null &"); 

But even using the /usr/bin/php-cli doesn’t work even though it tells me that. I have to use:

exec("/opt/php52/bin/php-cli $path_to_file > /dev/null 2>/dev/null &");

For this particular host for example.

解决方案

You can find the PHP binary path with this constant:

PHP_BINDIR

As of PHP 5.4, you can get the path to the executable actually running currently with this constant:

PHP_BINARY

http://php.net/manual/en/reserved.constants.php

相关文章