tags:

views:

33

answers:

2

How can I get the bin path of php from PHP? I saw it in phpinfo(); but I need another method that gets it in linux and windows systems.

+1  A: 

You can use:

$_SERVER['_']

also the constant

PHP_BINDIR gives the directory where the php executable is found.

Sample on Codepad and ideone

Looks like for security reasons $_SERVER values is not exposed.( I guess..Plz correct me if I'm wrong).

codaddict
In Windows, the PHP_BINDIR constant seems to point to "C:\php5" even if php is in a totally different directory, like for me "C:\dev\php". Here, PHP is setup as an apache module.
SirDarius
A: 

A method using environment variables, assuming the php executable is in the system path.

function getPHPExecutableFromPath() {
  $paths = explode(PATH_SEPARATOR, getenv('PATH'));
  foreach ($paths as $path) {
    $php_executable = $path . DIRECTORY_SEPARATOR . "php" . (isset($_SERVER["WINDIR"]) ? ".exe" : "");
    if (file_exists($php_executable) && is_file($php_executable)) {
       return $php_executable;
    }
  }
  return FALSE; // not found
}
SirDarius