简体   繁体   中英

How to get the full path to php interpreter / binary without shell access

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM