23

How can I get the binary path of php from PHP?

I saw it in phpinfo(), but I need another method that gets it in Linux and Windows systems.

1

9 Answers 9

21

You can use:

$_SERVER['_']

Also, the predefined constant PHP_BINDIR gives the directory where the PHP executable is found.

Sample on CodePad and Ideone.

It looks like, for security reasons, $_SERVER values are not exposed.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
The server I was on unfortunately returned NULL, I assume due to security reasons.
PHP_BINDIR worked for me on Debian. $_SERVER['_'] didn't.
PHP_BINDIR is set at compile time which means that it is wrong in most cases. PHP_BINARY is set at runtime. So use this instead.
PHP_BINARY (since php v5.4; see stackoverflow.com/a/9182368/3102305)
|
18

Linux Only

Use the "which" command to find php.

$phpPath = exec("which php");

Note this does not guarantee the same php executable that your web server may be using, but rather the first instance that was found while looking through the paths.

3 Comments

Good point but most hosting services don't allow exec at all.
Yes, some hosts do not, others do, I guess it depends your context.
This does not necessarily result in the correct php binary, since Apache and the command line might very well be using different ones.
11

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) {
    // We need this for XAMPP (Windows)
    if (strstr($path, 'php.exe') && isset($_SERVER["WINDIR"]) && file_exists($path) && is_file($path)) {
        return $path;
    }
    else {
        $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
}

3 Comments

@ggirtsou I don't get your edit... why would the Windows Path contain an entry with "php.exe" ? If it is something specific to XAMPP please at least add a meaningful comment in the code.
@ggirtsou then add a meaningful comment in the code, because it is certainly not standard behaviour
Those file_exists() are redundant
10

Maybe the best solution is in the Symfony process component:

PhpExecutableFinder.php and ExecutableFinder.php. In use:

<?php
    use Symfony\Component\Process\PhpExecutableFinder;

    $phpFinder = new PhpExecutableFinder;
    if (!$phpPath = $phpFinder->find()) {
        throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
    }

    return $phpPath;

Comments

4

Normally, in a simple default PHP installation under Windows, the php.ini file is located and loaded from the same directory of the PHP binary.

To simplify, Windows users:

echo dirname(php_ini_loaded_file()).DIRECTORY_SEPARATOR.'php.exe';

Voilà!

Of course, if you are using multiple .ini files, it may not work if the files are not into the same PHP binary directory. BTW, this may solve to most of cases. Windows developers running PHP from local development environment.

3 Comments

sorry forgot to say I am using mac
Are the last two sentences (directly) connected?
This solved it for me, thanks! But you might want to add chr(34) so it will be $runCommand = chr(34) . dirname(php_ini_loaded_file()).DIRECTORY_SEPARATOR.'php.exe' . chr(34); in order to support spaces in the path. Needed for example if like to run it in the background like in stackoverflow.com/a/73118719/4829915.
3

In Windows, using WAMP, you can use the ini variable - extension_dir - as it is placed in the PHP folder.

Like this:

echo str_replace('ext/', 'php.exe', ini_get('extension_dir'));

2 Comments

You shouldn't correlate this logic to that setting - even in WAMP or whatever - the setting might be changed intentionally.
You are absolutely correct. This would not work inside any framework or such that need to be able to work in every project out there. This was just the solution that worked best for me at the time for my local and server setup.
2

As of PHP 5.4 you can simply use the PHP_BINARY reserved constant.

Comments

0

It's very easy!

var_dump(getenv('PHPBIN'));

But it works only on Windows, so we should use this answer.

How did I get this? I just typed echo echo phpinfo(); and searched the php path there. Just see here:

How I found the PHP path

Then I just getting it here: php getenv and ... you see the result.

Comments

0

For Windows and XAMPP:

$php = getenv('PHPRC') . '/php.exe';

if(is_file($expected)){
   return $php;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.