Schwern
Schwern

Reputation: 164679

How do I find out the currently running PHP executable?

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

  1. I'm using lighttpd + FastCGI, not Apache + mod_php. So yes, there is a PHP binary.
  2. eval/include is not a solution because I'm spawning a server which has to live on beyond the request.

Things I've tried and don't work:

Thanks in advance.

Upvotes: 13

Views: 30982

Answers (7)

Witt
Witt

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

dsas
dsas

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

Wez Furlong
Wez Furlong

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

ariefbayu
ariefbayu

Reputation: 21979

what about: <?php exec("which php"); ?>

But, it's unix/linux only:D

Upvotes: -1

coding Bott
coding Bott

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

Lepidosteus
Lepidosteus

Reputation: 12027

The PHP_BINDIR constant gives you the directory where the php binary is

Upvotes: 16

chaos
chaos

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

Related Questions