Reputation: 164679
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.
Upvotes: 13
Views: 30982
Reputation: 517
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:
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.
Upvotes: -2
Reputation: 1660
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.
Upvotes: 4
Reputation: 4987
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.
Upvotes: 1
Reputation: 21979
what about:
<?php
exec("which php");
?>
But, it's unix/linux only:D
Upvotes: -1
Reputation: 4357
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.
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.
Upvotes: 0
Reputation: 12027
The PHP_BINDIR constant gives you the directory where the php binary is
Upvotes: 16
Reputation: 124267
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.
Upvotes: 3