14

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,

  1. Asking the host is out of the question
  2. 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.

3
  • Isn't it also in the $_SERVER? I'll go check Commented Jul 18, 2012 at 20:58
  • "Asking the host is out of the question" - is that meant technically, too? :) - possible duplicate of How do I find out the currently running PHP executable? Commented Jul 18, 2012 at 21:02
  • I can't ask the host because i won't know what it host it would be ahead of time. This code is for a plugin. Commented Jul 18, 2012 at 21:10

4 Answers 4

30

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

http://php.net/manual/en/reserved.constants.php

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

16 Comments

Thanks Brad but please look at the edited question for further clarification.
@Chris81, If you need to use a binary other than the one in use by your web server, then you need to know its path. The web server is not a mind reader, and only knows about what it has been configured with.
Thank you Brad. I was afraid that was the case.
See stackoverflow.com/questions/2372624/… - neither of these constants will reliably provide the path of the PHP executable or version executing for the current calling environment, on either Windows or Linux.
@Jake You realize that sometimes the binary running the content is the web server, right? That's how dynamically loaded modules work.
|
7

Linux users can try the whereis command.

I had this situation with a PHP IDE.

whereis php

Comments

3

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

1 Comment

The above is for the situation when your scripts run under PHP 5.3 and below. Naturally it's much easier and effiecient if PHP_BINARY const is available (as of PHP 5.4+).
0

its not common for a host to give root access to its users. Most probably, you can't access anything below /var/www

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.