简体   繁体   English

如何在 Windows 上确定 php.exe 的路径 - 搜索默认路径

[英]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.我目前正在 OS X 上为 Sublime Text 2 开发几个插件,我想让它们跨平台,这意味着我必须找出php.exe是否安装以及安装位置。

Right now I call /usr/bin/php in Python, which obviously works only on OS X and Linux:现在我在 Python 中调用/usr/bin/php ,这显然只适用于 OS X 和 Linux:

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

But on Windows, there seems to be no definitive default path for php.exe.但是在 Windows 上,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.因此,如果您在其他地方安装了 php.exe,请留下您的路径的评论,我会将其添加到上面的列表中。

Besides, there seems to be php.exe and php-cli.exe .此外,似乎还有php.exephp-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.首先检查 php-cli.exe,检查 php.exe,然后取第一个匹配项。 Is that correct or is there a better practice?这是正确的还是有更好的做法?

If the user has added PHP's bin folder to the system PATH , then you should just be able to try and execute php -v to check that it's present.如果用户已将 PHP 的 bin 文件夹添加到系统PATH ,那么您应该能够尝试执行php -v来检查它是否存在。

If you want to obtain the full path to the PHP executable and the target system is Windows Server 2003 or later (so Windows Vista , and Windows 7 ) then you could use the WHERE command, ie:如果您想获得 PHP 可执行文件的完整路径并且目标系统是Windows Server 2003或更高版本(因此Windows VistaWindows 7 ),那么您可以使用WHERE命令,即:

C:\> where php.exe
C:\Program Files (x86)\WAMP\bin\php\php5.3.5\php.exe

Also see possibly related question: Is there an equivalent of 'which' on the Windows command line?另请参阅可能相关的问题: Windows 命令行上是否有等效的“which”? . .

If you are really desperate to find any file on the user's computer, you could try executing the equivalent of a find - but it's going to be slooow !如果您真的很想在用户的计算机上找到任何文件,您可以尝试执行等效的find - 但它会很

C: && cd \ && dir /s /b php.exe

从 PHP 5.4 及更高版本开始,您可以使用 PHP_BINARY 常量。

On powershell or commad prompt php -r "echo PHP_VERSION;"在 powershell 或命令提示符 php -r "echo PHP_VERSION;" gives the version给出版本

See other PHP constantas: https://www.php.net/manual/es/reserved.constants.php查看其他 PHP 常量: https : //www.php.net/manual/es/reserved.constants.php

to answer your question回答你的问题

php -r "echo PHP_BINARY;" php -r "echo PHP_BINARY;" gives the full path to php.exe给出 php.exe 的完整路径

if needed, to remove the php.exe如果需要,删除 php.exe

php -r "echo str_replace('php.exe', '', PHP_BINARY);" php -r "echo str_replace('php.exe', '', PHP_BINARY);"

Use:采用:

C:\xampp\php\php.exe

It should work if your XAMPP instance is on D: or E: .如果您的XAMPP实例在D:E:上,它应该可以工作。 You change it accordingly from C: .您相应地从C:更改它。

Try尝试

echo PHP_BINDIR;

It works.有用。 I tested it on macOS as with PHP 5.6.我在 macOS 上和 PHP 5.6 一样对其进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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