Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out the currently running PHP executable?

Tags:

php

fastcgi

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:

  • $_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.

like image 503
Schwern Avatar asked Aug 13 '09 19:08

Schwern


4 Answers

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.

like image 152
chaos Avatar answered Oct 11 '22 04:10

chaos


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

like image 31
Lepidosteus Avatar answered Oct 11 '22 04:10

Lepidosteus


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.

like image 1
Wez Furlong Avatar answered Oct 11 '22 02:10

Wez Furlong


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.

like image 1
dsas Avatar answered Oct 11 '22 04:10

dsas