如何找出当前运行的 PHP 可执行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1274225/
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 do I find out the currently running PHP executable?
提问by Schwern
From inside a PHP program I want to know the location of the binary executing it. Perl has $^Xfor this purpose. Is there an equivalent in PHP?
从 PHP 程序内部,我想知道执行它的二进制文件的位置。Perl 已$^X为此目的。PHP中是否有等价物?
This is so it can execute a child PHP process using itself (rather than hard code a path or assume "php" is correct).
这样它就可以使用自己执行子 PHP 进程(而不是硬编码路径或假设“php”是正确的)。
UPDATE
更新
- I'm using lighttpd + FastCGI, not Apache + mod_php. So yes, there is a PHP binary.
- eval/include is not a solution because I'm spawning a server which has to live on beyond the request.
- 我使用的是 lighttpd + FastCGI,而不是 Apache + mod_php。所以是的,有一个 PHP 二进制文件。
- eval/include 不是解决方案,因为我正在生成一个必须在请求之外继续运行的服务器。
Things I've tried and don't work:
我尝试过但不起作用的事情:
$_SERVER['_']looks like what I want from the command line but its actually from an environment variable set by the shell of the last executed program. When run from a web server this is the web server binary.which phpwill not work because the PHP binary is not guaranteed to be the same one as is in the web server'sPATH.
$_SERVER['_']看起来像我想要的命令行,但它实际上来自上次执行程序的外壳程序设置的环境变量。从 Web 服务器运行时,这是 Web 服务器二进制文件。which php将不起作用,因为不能保证 PHP 二进制文件与 Web 服务器的PATH.
Thanks in advance.
提前致谢。
回答by Lepidosteus
The PHP_BINDIR constant gives you the directory where the php binary is
PHP_BINDIR 常量为您提供了 php 二进制文件所在的目录
回答by chaos
Yeah, $_SERVER['_']is what you're talking about, or as near as exists. The reason you're getting a Web server binary when it's run from the web is that /usr/bin/phphas nothing to do with the Web server's execution; what it's running is a separate SAPI. There's nothing from the web PHP instance to point to /usr/bin/phpbecause there's no reason for there to be.
是的,$_SERVER['_']这就是你所说的,或者尽可能接近存在。当您从 Web 运行时获得 Web 服务器二进制文件的原因是它/usr/bin/php与 Web 服务器的执行无关;它运行的是一个单独的 SAPI。Web PHP 实例中/usr/bin/php没有任何内容可指向,因为没有理由存在。
回答by Wez Furlong
The PHP_BINDIRconstant is probably the easiest thing to use; the next best thing I could come up with is basically re-creating that bindir path from the extension_dirconfiguration setting:
该PHP_BINDIR常数可能是使用最简单的事; 我能想到的下一个最好的事情基本上是从extension_dir配置设置中重新创建那个 bindir 路径:
$phpbin = preg_replace("@/lib(64)?/.*$@", "/bin/php", ini_get("extension_dir"));
It has a regex in it, so it feels more like your native perl(!) but otherwise is not especially optimal.
它有一个正则表达式,所以感觉更像你的原生 perl(!),但除此之外并不是特别理想。
回答by dsas
In PHP5.4 you can use the PHP_BINARY constant, it won't work via mod_php or similar but will via CGI etc.
在 PHP5.4 中,您可以使用 PHP_BINARY 常量,它不能通过 mod_php 或类似方式工作,但可以通过 CGI 等工作。
For earlier versions of PHP readlink('/proc/self/exe');will probably be fine, again it won't work via mod_php.
对于早期版本的 PHPreadlink('/proc/self/exe');可能会很好,同样它不能通过 mod_php 工作。
回答by Bernd Ott
Depending on the way php is installed you CANT find the php executable. if php is running as a module for the webserver like apache module, then there is no binary you can call. you can take a look into php_info() it lists everything. may also the path to php. within that path you can assume a php binary.
根据 php 的安装方式,您无法找到 php 可执行文件。如果 php 像 apache 模块一样作为网络服务器的模块运行,则没有可以调用的二进制文件。您可以查看 php_info() 它列出了所有内容。也可能是php的路径。在该路径中,您可以假设一个 php 二进制文件。
but why do you want to call a extra process? you can execute other php files by include command or eval. there is no reason to spawn a new process.
但是你为什么要调用一个额外的进程呢?你可以通过 include 命令或 eval 执行其他 php 文件。没有理由产生一个新进程。
回答by Witt
I've been looking for the php7 executable on my mac (OSX El Capitan) in order to configure and install xdebug (needed to find the right version of phpize to run). None of the solutions I found worked for me, so I just ended out searching for it:
我一直在我的 mac (OSX El Capitan) 上寻找 php7 可执行文件,以便配置和安装 xdebug(需要找到正确版本的 phpize 才能运行)。我找到的所有解决方案都不适合我,所以我最终搜索了它:
find / -name php -print
I knew (from phpinfo()) that I was running php7, so I was able to infer the correct directory from the options presented by find.
我知道(从 phpinfo() )我正在运行 php7,所以我能够从 find 提供的选项中推断出正确的目录。
回答by ariefbayu
what about:
<?php
exec("which php");
?>
关于什么:
<?php
exec("which php");
?>
But, it's unix/linux only:D
但是,它仅适用于 unix/linux:D

