问题
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'<br>
pluginpath = sublime.packages_path() + '/HtmlTidy/tidy.php'<br>
retval = os.system( '%s "%s"' % ( phppath, scriptpath ) )
But on Windows, there seems to be no definitve default path for php.exe. The more I googled for it, the more possibilies 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 possibilties - 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 trough each possible path, check first for php-cli.exe, then check for php.exe and take the first match. Is that correct or is there a better practice?
回答1:
If the user has defined 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.
If you want to obtain the full path to the php executable and the target system is Windows 2003 or later (so Vista, and 7) then you could use the WHERE
command, ie:
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?.
If you are really desperate to find any file on the user's computer, you could try executing the equivilent of a find
- but it's gonna be slooow!
C: && cd \ && dir /s /b php.exe
回答2:
From PHP 5.4 and later, you can use the PHP_BINARY constant.
回答3:
C:\xampp\php\php.exe
Should work if your xampp is on D: or E: you change accordingly from C:
回答4:
try
echo PHP_BINDIR;
It works, I tested it on MacOS as with php 5.6
来源:https://stackoverflow.com/questions/9645545/how-to-determine-path-to-php-exe-on-windows-search-default-paths