问题
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:
You can use:
$_SERVER['_']
Also, the predefined constant PHP_BINDIR gives the directory where the php executable is found.
Sample on Codepad and ideone.
Looks like, for security reasons, $_SERVER values are not exposed. (I guess. Please correct me if I'm wrong).
回答2:
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:
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
}
回答4:
Maybe the best solution is in the Symfony process component: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Process/PhpExecutableFinder.php and https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Process/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;
回答5:
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'));
回答6:
Normally, in a simple default PHP installation under Windows, the php.ini file is located and loaded from same directory of 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, 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.
回答7:
It's very easy!
var_dump(getenv('PHPBIN'));
But it works only in windows, so we should use this ansver - https://stackoverflow.com/a/3889630/2574125
How i got this? I just typed echo echo phpinfo(); and searched there php path. Juse see here:
then i just getting here: php getenv and ... you see the result. Use it, folks.
回答8:
Windows, XAMPP
$php = getenv('PHPRC') . '/php.exe';
if(is_file($expected)){
return $php;
}
来源:https://stackoverflow.com/questions/3889486/how-to-get-the-path-of-the-php-bin-from-php