php 如何从PHP获取PHP BIN的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3889486/
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
How to get the path of the PHP BIN from PHP?
提问by Wiliam
How can I get the bin path of php from PHP? I saw it in phpinfo(); but I need another method that gets it in linux and windows systems.
如何从PHP获取php的bin路径?我在 phpinfo(); 中看到了 但我需要另一种在 linux 和 windows 系统中获取它的方法。
回答by codaddict
You can use:
您可以使用:
$_SERVER['_']
Also, the predefined constant PHP_BINDIR
gives the directory where the php executable is found.
此外,预定义的常量PHP_BINDIR
给出了 php 可执行文件所在的目录。
Looks like, for security reasons, $_SERVER
values are not exposed. (I guess. Please correct me if I'm wrong).
看起来,出于安全原因,$_SERVER
值没有公开。(我猜。如果我错了,请纠正我)。
回答by SeanDowney
Linux OnlyUse the "which" command to find php.
Linux Only使用“which”命令来查找php。
$phpPath = exec("which php");
Note this does not guarantee the same php executable that your web server may be using, but rather the first instance that was found while looking through the paths.
请注意,这并不能保证您的 Web 服务器可能正在使用相同的 php 可执行文件,而是保证在查看路径时找到的第一个实例。
回答by SirDarius
A method using environment variables, assuming the php executable is in the system path.
一种使用环境变量的方法,假设 php 可执行文件在系统路径中。
function getPHPExecutableFromPath() {
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
// we need this for XAMPP (Windows)
if (strstr($path, 'php.exe') && isset($_SERVER["WINDIR"]) && file_exists($path) && is_file($path)) {
return $path;
}
else {
$php_executable = $path . DIRECTORY_SEPARATOR . "php" . (isset($_SERVER["WINDIR"]) ? ".exe" : "");
if (file_exists($php_executable) && is_file($php_executable)) {
return $php_executable;
}
}
}
return FALSE; // not found
}
回答by Krisztián Ferenczi
Maybe the best solution is in the Symfony process component: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Process/PhpExecutableFinder.phpand https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Process/ExecutableFinder.php. In use:
也许最好的解决方案是在 Symfony 进程组件中:https: //github.com/symfony/symfony/blob/master/src/Symfony/Component/Process/PhpExecutableFinder.php和https://github.com/symfony/symfony /blob/master/src/Symfony/Component/Process/ExecutableFinder.php。正在使用:
<?php
use Symfony\Component\Process\PhpExecutableFinder;
$phpFinder = new PhpExecutableFinder;
if (!$phpPath = $phpFinder->find()) {
throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
}
return $phpPath;
回答by John
In windows, using wamp, you can use the ini variable - extension_dir - as it is placed in the php folder.
在 Windows 中,使用 wamp,您可以使用 ini 变量 - extension_dir - 因为它位于 php 文件夹中。
like this:
像这样:
echo str_replace('ext/', 'php.exe', ini_get('extension_dir'));
回答by Daniel Omine
Normally, in a simple default PHP installation under Windows, the php.ini file is located and loaded from same directory of PHP binary.
通常,在 Windows 下的简单默认 PHP 安装中,php.ini 文件位于并从 PHP 二进制文件的同一目录加载。
To simplify, Windows users:
为简化起见,Windows 用户:
echo dirname(php_ini_loaded_file()).DIRECTORY_SEPARATOR.'php.exe';
voilà!
瞧!
Of course, if you are using multiple ini files, may not work if the files are not into the same PHP binary directory. BTW, this may solve to most of cases. Windows developers running PHP from local development environment.
当然,如果您使用多个 ini 文件,如果这些文件不在同一个 PHP 二进制目录中,则可能不起作用。顺便说一句,这可能会解决大多数情况。Windows 开发人员从本地开发环境运行 PHP。
回答by aftamat4ik
It's very easy!
这很容易!
var_dump(getenv('PHPBIN'));
But it works only in windows, so we should use this ansver - https://stackoverflow.com/a/3889630/2574125
但它只适用于 Windows,所以我们应该使用这个 ansver - https://stackoverflow.com/a/3889630/2574125
How i got this? I just typed echo echo phpinfo();
and searched there php path. Juse see here:
我怎么得到这个?我只是输入 echoecho phpinfo();
并在那里搜索了 php 路径。请看这里:
then i just getting here: php getenvand ... you see the result. Use it, folks.
然后我就到了这里:php getenv和...你会看到结果。使用它,伙计们。
回答by ymakux
Windows, XAMPP
视窗,XAMPP
$php = getenv('PHPRC') . '/php.exe';
if(is_file($expected)){
return $php;
}
回答by Joe
I know I'm late, but…
我知道我迟到了,但是……
…as of PHP 5.4 you can simply use the PHP_BINARY
constant.
…从 PHP 5.4 开始,您可以简单地使用PHP_BINARY
常量.