且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-10 09:18:28

你可以用这个常量找到 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