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

时间:2021-05-08 20:44:27

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

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

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.

但我需要首先获得路径,以便将其分配给foo。

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

如果你有一个适用于Windows和nix的解决方案甚至更好,但如果没有,nix就可以了。

Before you ask,

在你问之前,

  1. Asking the host is out of the question
  2. 询问主持人是不可能的

  3. No shell access
  4. 没有shell访问权限

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:

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

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:

但即使使用/ usr / bin / php-cli也无法正常工作,即使它告诉我。我必须使用:

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

For this particular host for example.

例如,对于这个特定的主机。

4 个解决方案

#1


26  

You can find the PHP binary path with this constant:

您可以使用此常量找到PHP二进制路径:

PHP_BINDIR

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

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

PHP_BINARY

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

#2


5  

Linux users can try the whereis command.

Linux用户可以尝试whereis命令。

I had this situation with a PHP IDE.

我有一个PHP IDE的情况。

whereis php

#3


3  

On Windows I would suggest the following snippet:

在Windows上,我建议使用以下代码段:

<?php
$pid = getmypid();
$output = shell_exec(sprintf('tasklist /nh /fo csv /fi "PID eq %d"', $pid));
$processInfo = explode(',', $output);
echo PHP_BINDIR . DIRECTORY_SEPARATOR . trim($processInfo[0], '"');

On unix the shell command should probably make use of ps

在unix上,shell命令应该可以使用ps

#4


0  

its not common for a host to give root access to its users. Most probably, you can't access anything below /var/www

主机为其用户提供root访问权限并不常见。最有可能的是,您无法访问/ var / www下面的任何内容

#1


26  

You can find the PHP binary path with this constant:

您可以使用此常量找到PHP二进制路径:

PHP_BINDIR

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

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

PHP_BINARY

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

#2


5  

Linux users can try the whereis command.

Linux用户可以尝试whereis命令。

I had this situation with a PHP IDE.

我有一个PHP IDE的情况。

whereis php

#3


3  

On Windows I would suggest the following snippet:

在Windows上,我建议使用以下代码段:

<?php
$pid = getmypid();
$output = shell_exec(sprintf('tasklist /nh /fo csv /fi "PID eq %d"', $pid));
$processInfo = explode(',', $output);
echo PHP_BINDIR . DIRECTORY_SEPARATOR . trim($processInfo[0], '"');

On unix the shell command should probably make use of ps

在unix上,shell命令应该可以使用ps

#4


0  

its not common for a host to give root access to its users. Most probably, you can't access anything below /var/www

主机为其用户提供root访问权限并不常见。最有可能的是,您无法访问/ var / www下面的任何内容