From inside a PHP program I want to know the location of the binary executing it. Perl has $^X
for this purpose. Is there an equivalent in PHP?
This is so it can execute a child PHP process using itself (rather than hard code a path or assume "php" is correct).
UPDATE
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 php
will not work because the PHP binary is not guaranteed to be the same one as is in the web server's PATH
.Thanks in advance.
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/php
has 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/php
because there's no reason for there to be.
The PHP_BINDIR constant gives you the directory where the php binary is
The PHP_BINDIR
constant 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_dir
configuration setting:
$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.
In PHP5.4 you can use the PHP_BINARY constant, it won't work via mod_php or similar but will via CGI etc.
For earlier versions of PHP readlink('/proc/self/exe');
will probably be fine, again it won't work via mod_php.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With