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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11550193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:45:21  来源:igfitidea点击:

How to get the full path to php interpreter / binary without shell access

phppathinterpreter

提问by Chris81

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. No shell access
  1. 问主持人是不可能的
  2. 没有外壳访问

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.

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

回答by Brad

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

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

回答by parera riddle

Linux users can try the whereiscommand.

Linux 用户可以尝试该whereis命令。

I had this situation with a PHP IDE.

我在 PHP IDE 中遇到过这种情况。

whereis php

回答by user725408

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

回答by Pablo Mescher

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 下的任何内容