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.

Chris81
  • 426
  • 2
  • 4
  • 16
  • Isn't it also in the $_SERVER? I'll go check – Mathieu Dumoulin Jul 18 '12 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?](http://stackoverflow.com/questions/1274225/how-do-i-find-out-the-currently-running-php-executable) – hakre Jul 18 '12 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. – Chris81 Jul 18 '12 at 21:10

4 Answers4

31

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

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thanks Brad but please look at the edited question for further clarification. – Chris81 Jul 18 '12 at 21:16
  • 1
    @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. – Brad Jul 18 '12 at 21:52
  • Thank you Brad. I was afraid that was the case. – Chris81 Jul 18 '12 at 23:47
  • See https://stackoverflow.com/questions/2372624/get-current-php-executable-from-within-script - 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 Dec 16 '17 at 23:56
  • @Jake I don't believe you. Can you prove it? I see no such proof in any of the answers in the question you linked to. The documentation for `PHP_BINARY` explicitly states, "Specifies the PHP binary path during script execution. Available since PHP 5.4." – Brad Dec 17 '17 at 02:17
  • @Brad See the comments below the answers. – Jake Dec 17 '17 at 17:55
  • @Jake I did. I didn't see anything that matches what you are claiming. Write some proof code? – Brad Dec 17 '17 at 18:21
  • `error_log('PHP_BINDIR is "' . PHP_BINDIR . '"'); error_log('PHP_BINARY is "' . PHP_BINARY . '"');` There are enough comments from various people to indicate that on some systems these constants do not provide the correct value. Hence they are not reliable. – Jake Dec 19 '17 at 22:17
  • @Jake `PHP_BINDIR` and `PHP_BINARY` are two different things. I still don't see any proof code, nor do I see a single comment backing up what you're saying, directly. – Brad Dec 20 '17 at 02:04
  • Proof code in my previous comment above. Obviously you'll need to run it on various systems/configurations, then look at the error log output file. Comments/answers like "Unfortunately PHP_BINARY is returning the httpd binary" and "Will always return C:\php no matter the reality" are there if you look. If you can't see them I cannot help you. – Jake Dec 21 '17 at 01:37
  • 1
    @Jake You realize that sometimes the binary running the content *is* the web server, right? That's how dynamically loaded modules work. – Brad Dec 21 '17 at 01:40
7

Linux users can try the whereis command.

I had this situation with a PHP IDE.

whereis php
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
parera riddle
  • 148
  • 4
  • 10
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

user725408
  • 336
  • 3
  • 8
  • 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+). – user725408 Feb 20 '15 at 07:41
0

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

Pablo Mescher
  • 26,057
  • 6
  • 30
  • 33