Cómo determinar la ruta a php.exe en Windows - buscar rutas predeterminadas

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 está instalado.

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 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:\program files\php\php.exe

That's already quite a collection, but what I'm asking for is either a completar 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 y 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?

preguntado el 10 de marzo de 12 a las 09:03

I would just search for it in the Windows PATH, and then if it's not there, prompt the user for it. Your list can never be complete. And, what if the user has multiple paths to a php.exe? I have 5 or 6 php.exe right now. -

Eche un vistazo a la PHPRC environment variable. It contains the directory where php.exe is located. It's often available on windows systems. -

you might be able to search the registry, but I'm no expert on it so I ccouldn't say how. -

5 Respuestas

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.

If you want to obtain the full path to the PHP executable and the target system is Windows Server 2003 or later (so Windows Vistay Windows 7) then you could use the WHERE command, i.e.:

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

Also see possibly related question: ¿Existe un equivalente de 'which' en la línea de comandos de Windows?.

If you are really desperate to find cualquier file on the user's computer, you could try executing the equivalent of a find - but it's going to be lento!

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

Respondido 06 Oct 20, 19:10

where doesn't find my php.exe. Indeed it seems only to look in PATH and the current directory. - Kos

@Kos could you tell us, where tu php.exe actually is? - Matt

Thank you JonnyReeves, where would probably do the job, but the first answer on the link you posted led me to this python script which seems to deal quite well with PATH and PATHEXT: nedbatchelder.com/code/utilities/wh_py.html - since the plugin for Sublime Text 2 is in python, I'm going to use some lines of code from it to minimize calling external programs. - Matt

@Mat a regular WAMP installation like mentioned before, C:\wamp\bin\php\php5.3.9\php.exe - Kos

where /? says "By default, the search is done along the current directory and in the paths specified by the PATH environment variable." - Kos

From PHP 5.4 and later, you can use the PHP_BINARY constant.

Respondido 01 Abr '12, 22:04

For all us noobies out there, it would be useful if this was updated with a longer explanation. - bram vanroy

echo PHP_BINARY; but it's giving me nothing on Linux. - Sebastián Grignoli

In my case it returns C:\xampp\apache\bin\httpd.exe which does not execute a file, while C:\xampp\php\php.exe does - ymakux

On powershell or commad prompt php -r "echo PHP_VERSION;" gives the version

See other PHP constantas: https://www.php.net/manual/es/reserved.constants.php

para responder tu pregunta

php -r "echo PHP_BINARY;" gives the full path to php.exe

if needed, to remove the php.exe

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

Respondido el 09 de enero de 21 a las 11:01



It should work if your XAMPP instance is on D: or E:. You change it accordingly from C:.

Respondido 06 Oct 20, 19:10



It works. I tested it on macOS as with PHP 5.6.

Respondido 06 Oct 20, 19:10

Question is about Windows not Mac. This does not work - Ryall

Good to know. Thanks for sharing your experience. What system configuration did you try it on? Php vercsion and so on... - Yevgeni Afanasyev

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.