How can I get the full path to php interpreter from a php script (no command line access).
What I need to do is:
$foo = "/usr/bin/php";
echo $foo;
But I need to get the path first so I can assign it to foo.
If you have a solution that works on both Windows and nix even better but if not, nix would be fine.
Before you ask,
- Asking the host is out of the question
- No shell access
The problem is that using whatever it outputs doesn't work. For example PHP_BINDIR will output /usr/bin but using /usr/bin/php won't help. The full code is:
exec("php-cli $path_to_file > /dev/null 2>/dev/null &");
But even using the /usr/bin/php-cli doesn’t work even though it tells me that. I have to use:
exec("/opt/php52/bin/php-cli $path_to_file > /dev/null 2>/dev/null &");
For this particular host for example.
You can find the PHP binary path with this constant:
PHP_BINDIR
As of PHP 5.4, you can get the path to the executable actually running currently with this constant:
PHP_BINARY
Linux users can try the whereis
command.
I had this situation with a PHP IDE.
whereis php
On Windows I would suggest the following snippet:
<?php
$pid = getmypid();
$output = shell_exec(sprintf('tasklist /nh /fo csv /fi "PID eq %d"', $pid));
$processInfo = explode(',', $output);
echo PHP_BINDIR . DIRECTORY_SEPARATOR . trim($processInfo[0], '"');
On unix the shell command should probably make use of ps
its not common for a host to give root access to its users. Most probably, you can't access anything below /var/www
来源:https://stackoverflow.com/questions/11550193/how-to-get-the-full-path-to-php-interpreter-binary-without-shell-access