Find path to php executable

How do I find out the currently running PHP executable?

Yes this works. I have both /usr/bin/php and XAMPP’s own php, and executing each of them yields their correct path to the binary.

PHP_BINDIR gives the path that PHP binaries were put in when compiling, not the location of the binaries at run time. They will often be the same but not always.

Yeah, $_SERVER[‘_’] is what you’re talking about, or as near as exists. The reason you’re getting a Web server binary when it’s run from the web is that /usr/bin/php has nothing to do with the Web server’s execution; what it’s running is a separate SAPI. There’s nothing from the web PHP instance to point to /usr/bin/php because there’s no reason for there to be.

@schwern: but if the executable is the web server and php is just used as a library, why would there be a reason to point to a php binary if it isn’t used. There is a difference with how web servers use perl because in most cases the interface is CGI and that means running the perl executable (there are exceptions). I’d say your best bet is a configuration string or use eval() if that is good enough for your needs.

Читайте также:  Python разделить массив пополам

@Schwern: If you deleted /usr/bin/php , your Web server would continue to process PHP files just fine. I don’t know how to tell you why there’s no technical reason for a linkage between them any more clearly than that.

There is a world beyond Apache. I’m using lighttpd + FastCGI. PHP is not embedded in the web server. In fact, its not even on the same machine!

The thing is that using it like that is the exception. It is not «beyond Apache», more or less every server uses php the same way as apache does as it is the most efficient way. Good thing you edited your post to indicate that it wasn’t a standard solution but a very installation specific one you were after.

Источник

How to determine path to php.exe on Windows — search default paths

I’m currently developing a couple of plugins for Sublime Text 2 on OS X and I would like to make them cross platform, meaning I have to find out if and where php.exe is installed. Right now I call /usr/bin/php in Python, which obviously works only on OS X and Linux:

phppath = '/usr/bin/php' 
pluginpath = sublime.packages_path() + '/HtmlTidy/tidy.php'
retval = os.system( '%s "%s"' % ( phppath, scriptpath ) )

But on Windows, there seems to be no definitive default path for php.exe. The more I googled for it, the more possibilities showed up. So far I think I would have to check each of the following paths for existence:

c:\php\php.exe c:\php5\php.exe c:\windows\php.exe c:\program files\php\php.exe c:\wamp\bin\php\php5\php.exe c:\xampp\php\php.exe 

That’s already quite a collection, but what I’m asking for is either a complete list covering all possibilities — or another way to figure it out that should be as robust as checking each possible path. So if you have php.exe installed in some place other than these, please leave a comment with your path and I will add it to the list above. Besides, there seems to be php.exe and php-cli.exe . I guess it would be OK to loop through each possible path. Check first for php-cli.exe, check for php.exe, and take the first match. Is that correct or is there a better practice?

Источник

How To Find the Executable Path of PHP

Ubuntu 9

In this article, we will discuss how to find the executable path of PHP on your system. This is an important step when you’re configuring your web server or trying to run a PHP script from the command line.

To find the executable path of PHP, you can use the which command in Unix and Linux. Simply type which php in the terminal or command line, and it will display the path to the PHP executable. However, it’s important to note that the returned path might be a symbolic link, not the actual executable file. To find the actual executable, you can follow the symbolic link using the ls -l command.

What is the PHP Executable Path?

The PHP executable path is the location of the PHP binary file on your system. This file is responsible for interpreting PHP code and executing it. The path to this file is needed when you are configuring your web server or trying to run PHP scripts from the command line.

Finding the PHP Executable Path

To find the PHP executable path, you will need to use the terminal or command line interface on your system. The steps may slightly vary depending on your operating system, but the general process remains the same.

Using the which Command

The which command in Unix and Linux is used to locate the executable file associated with the given command. You can use it to find the path of the PHP executable by typing:

This command will display the path to the PHP executable. For example, it might return something like /usr/bin/php .

However, it’s important to note that the returned path might be a symbolic link, not the actual executable file. A symbolic link is a file that points to another file or directory. To find out if the PHP executable is a symbolic link, you can use the ls -l command:

If the file is a symbolic link, the command will display something like this: /usr/bin/php -> /etc/alternatives/php .

Finding the Actual Executable

To find the actual PHP executable, you need to follow the symbolic link. You can do this by using the ls -l command again:

This command will reveal the actual path of the PHP executable, which might be something like /usr/bin/php7.2 .

Getting More Information About the Executable

If you want to know more about the PHP executable file, you can use the file command:

This command will provide you with detailed information about the PHP executable, such as its type, architecture, and build details.

Conclusion

Finding the PHP executable path is a crucial step in configuring your web server or running PHP scripts from the command line. By using the which , ls -l , and file commands, you can locate the PHP executable and gather important information about it. Remember that the specific paths and commands may vary depending on your system configuration.

We hope this article has been helpful in guiding you through the process of finding the PHP executable path. For more information about PHP and its configuration, you can visit the official PHP website.

It is important to find the PHP executable path because it is needed for configuring your web server or running PHP scripts from the command line.

You can find the PHP executable path by using the which command in Unix and Linux. Simply type which php in the terminal or command line to display the path to the PHP executable.

If the returned path is a symbolic link, you can use the ls -l command to follow the link and find the actual path of the PHP executable.

To get more information about the PHP executable, you can use the file command followed by the path to the PHP executable. This will provide you with detailed information about the file, such as its type, architecture, and build details.

Источник

How can I get the current PHP executable from within a script?

I want the inner script to be run with the same PHP version. In Perl, I would use $^X to get the Perl executable. It appears there isn’t any such variable in PHP. Right now, I’m using $_SERVER[‘_’] , because Bash (and zsh) sets the environment variable $_ to the last-run program. But, I’d rather not rely on a shell-specific idiom. UPDATE: Version differences are but one problem. If PHP isn’t in PATH, for example, or isn’t the first version found in PATH, the suggestions to find the version information won’t help. Additionally, csh and variants appear to not set the $_ environment variable for their processes, so the workaround isn’t applicable there. UPDATE 2: I was using $_SERVER[‘_’] , until I discovered that it doesn’t do the right thing under xargs (which makes sense. zsh sets it to the command it ran, which is xargs , not php5 , and xargs doesn’t change the variable). I am falling back to using:

$version = explode('.', phpversion()); $phpcli = "php"; 

Is it possible to simply conditionally include the other PHP file? That’s the easiest way to insure the included code runs with the same interpreter as the including code.

Interesting point. In this particular case, part of the reason it’s being run as a subprocess is that the inner script calls ‘exit()’ at various places.

9 Answers 9

It is worth noting that now in PHP 5.4+ you can use the predefined constant PHP_BINARY:

PHP_BINARY

Specifies the PHP binary path during script execution. Available since PHP 5.4.

This returns C:\php on Windows where no such file or directory exists (PHP 7.1.11, Apache 2.4.29, Windows 7 Pro) — as, as noted in a comment on another answer, does PHP_BINDIR .

And on the hosting company’s Linux box it also returns the same as PHP_BINDIR . Here it is /usr/bin and does contain a php executable, but that is the version 5.3 executable not the 7.0.25 version of the calling environment. So I don’t think this answer is useful.

For php-fpm (on Debian) this returns /usr/sbin/php-frpm7.3 which cannot be used to launch any PHP script. While technically correct, it’s completely useless for OP’s purpose.

On my server I’ve PHP 5.3.14.

I’ve found a predefined constant: PHP_BIN_DIR

Then, supposing the file name of the executable file is always ‘php’, $php_cmd = PHP_BIN_DIR.’/php’ points to my PHP executable file.

According to the manual, which doesn’t say when it was added, it’s PHP_BINDIR (no second underscore). This is a pretty decent answer. Thanks.

I upvoted the answer and the comment but took the upvotes back after trying it in Windows. Will always return C:\php no matter the reality.

Nice answer, however, it won’t work as expected if multiple versions of PHP are installed on a Linux system, since they will likely all be in /usr/bin .

Update; nowadays PHP_BINARY works with XAMPP as well (Tested with XAMPP that comes with PHP 7.4).

Old Answer

Unfortunately PHP_BINARY is returning the httpd binary (on Windows XAMPP), so I’m back to using paths.

 if (defined('PHP_BINARY') && PHP_BINARY && in_array(PHP_SAPI, array('cli', 'cli-server')) && is_file(PHP_BINARY)) < return PHP_BINARY; >else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') < $paths = explode(PATH_SEPARATOR, getenv('PATH')); foreach ($paths as $path) < if (substr($path, strlen($path)-1) == DIRECTORY_SEPARATOR) < $path = substr($path, 0, strlen($path)-1); >if (substr($path, strlen($path) - strlen('php')) == 'php') < $response = $path.DIRECTORY_SEPARATOR . 'php.exe'; if (is_file($response)) < return $response; >> else if (substr($path, strlen($path) - strlen('php.exe')) == 'php.exe') < if (is_file($response)) < return $response; >> > > else < $paths = explode(PATH_SEPARATOR, getenv('PATH')); foreach ($paths as $path) < if (substr($path, strlen($path)-1) == DIRECTORY_SEPARATOR) < $path = substr($path, strlen($path)-1); >if (substr($path, strlen($path) - strlen('php')) == 'php') < if (is_file($path)) < return $path; >$response = $path.DIRECTORY_SEPARATOR . 'php'; if (is_file($response)) < return $response; >> > > return null; 

Has someone tried it? Does it work? Consistently and across Win and Unix? The code is ugly, but if it works, I’ll normalize it.

I know this is an old answer an no one has answers @XedinUnknown but I can confirm this works perfectly on Windows running XAMPP. Has anyone else tested this in more environments?

Okay, so this is ugly, but it works on Linux:

It doesn’t look like there’s any easy way to do this for Windows. Some Windows executables like tasklist can give you the name of the executable, but not the full path to where the executable is. I was only able to find examples for finding the full path given a PID for C++, AutoHotkey and the like. If anyone has any suggestions on where else I could look, let me know.

PS: To get the PID in PHP for Windows, you apparently have to call getmypid().

I was looking into doing this too. I thought I might be able to use the w32api or ffi PECL extensions to call the GetModuleFileName API function, but neither extension seems to be actively maintained at the moment.

You could use phpversion() to get the current version of PHP before you execute the «inner» script.

After being dissatisfied with the answers, I came up with my own. It tries to use PHP_BINARY if it looks reasonable, otherwise it looks for an interpreter with the same version as the currently running one.

/** * Return a suitable PHP interpreter that is likely to be the same version as the * currently running interpreter. This is similar to using the PHP_BINARY constant, but * it will also work from within mod_php or PHP-FPM, in which case PHP_BINARY will return * unusable interpreters. * * @return string */ public function getPhpInterpreter(): string < static $cachedExecutable = null; if ($cachedExecutable !== null) < return $cachedExecutable; >$basename = basename(PHP_BINARY); // If the binary is 'php', 'php7', 'php7.3' etc, then assume it's a usable interpreter if ($basename === 'php' || preg_match('/^php\d+(?:\.\d+)*$/', $basename)) < return PHP_BINARY; >// Otherwise, we might be running as mod_php, php-fpm, etc, where PHP_BINARY is not a // usable PHP interpreter. Try to find one with the same version as the current one. $candidates = [ 'php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION, 'php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION, 'php' . PHP_MAJOR_VERSION, ]; $envPath = $_SERVER['PATH'] ?? ''; $paths = $envPath !== '' ? explode(':', $envPath) : []; if (!in_array(PHP_BINDIR, $paths, true)) < $paths[] = PHP_BINDIR; >foreach ($candidates as $candidate) < foreach ($paths as $path) < $executable = $path . DIRECTORY_SEPARATOR . $candidate; if (is_executable($executable)) < $cachedExecutable = $executable; return $executable; >> > // Fallback, if nothing else can be found $cachedExecutable = 'php'; return $cachedExecutable; > 

Let me know if this works for you. I tested it on Debian Buster, from CLI and from FPM.

Источник

Оцените статью