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

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 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?

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 Vista, and 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: 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 equivalent of a find - but it's going to be slooow!
C: && cd \ && dir /s /b php.exe

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
to answer your question
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);"

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

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

I found a way for you to find out where the php.exe is stored. In C:, click the search bar and search for "php.exe". Then, there will appear an application called php and click it. Then, cmd will open and in the cmd tab it will say php.exe at the end. That's where your php.exe is and you can put the link address of the file in VS Code.

Try
echo PHP_BINDIR;
It works. I tested it on macOS as with PHP 5.6.

Related

Laravel and Mamp Localhost Error 500 Mac

So after a fresh install of laravel (v5.4) and using Mamp as my local server I just get (in chrome)
The localhost page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
I've tried countless things like changing permissions on storage or everything within the laravel directory.
Done composer update/install in the directory.
I'm running PHP v7.0.12 on my Mac (10.11.6). Cant think of anything else that would be causing this?
If I create another directory which a simple index.php with '' it loads absolutely fine so what is causing an issue with laravel?
Here's pics of my mamp setup
http://i.imgur.com/xT3mMZO.png
http://i.imgur.com/ddz9uij.png
Open up terminal and type:
tail -n 20 /Applications/MAMP/logs/php_error.log
This may give you some clue as to what is going on.
Hope this helps, happy coding :)
I just joined stackoverflow yesterday so I do not have enough reputation to comment. I write an answer here.
here is the short answer:
run :
PHP_VERSION=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
echo "export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH" >> .bash_profile
source ~/.bash_profile
check your php version by running php -v. close the terminal. Open another terminal and check php version again.if you see php 5 again then there are 2 possibilities:
First: you added the /path/to/php7 after /path/to/php5 and php 5 is loaded first.
Second: system loads another bash file first which your php 5 path is mentioned in it. I know that this sounds confusing having several bash file but it is. Please search for other files like bashrc or similars.
This is the long answer for anyone else:
first of all, if you are on mac, I highly suggest you to use valet to get rid of all these things.
But regarding the problem:
As you already noted that it is not a laravel issue. the php version that your machine uses is under the min required version by laravel. You have 2 versions of php at the moment. MacOs is shipped with php installed on that. I think it is the same php 5.5.36 that is making problem for you. Since Laravel 5.4, you need to use PHP >= 5.6.4.
Although you have installed parallel php 7 on your machine, you need also to check which php your machine is reading from by running which php. after that remember to add your path to php 7 installed to bash_profile or other bash files on your mac(you can have 4 or 5 bash files).
Considering you have only one bash file named as bash_profile and you are going to export the path to your php 7 to bash profile, If you run following command, that appends the path to the end of file
PHP_VERSION=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
echo "export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH" >> .bash_profile
remember to use source ~/.bash_profile in order to make changes effected.
Alternatively you can open the bash_profile file in your text editor by running:
open -a /Applications/TextEdit.app ~/.bash_profile
and add the path manually before the /path/to/php5 path.
finally you must check your php version by running php -v.
close the terminal. Open another terminal and check php version again. If you see php 7 then you are all fine. But if you see php 5 again then there are 2 possibilities:
First: you added the /path/to/php7 after /path/to/php5 and php 5 is loaded first.
Second: system loads another bash file first which your php 5 path is mentioned in it. I know that this sounds confusing having several bash file but it is. Please search for other files like bashrc or similars.

cygwin: can't execute PHP script via PATH

I have written a PHP script "toalpha" (purpose not relevant to this question). The script is in $HOME/bin. If my CWD is $HOME/bin, I can execute it by typing "./toalpha". But if I simply type "toalpha", I get a message:
Could not open input file: /c/barry/bin/toalpha
Some information that may be relevant:
. I recently upgraded my desktop from WinXP to Win7.
. This is the first time that I have made my own userid be an ordinary user instead of an admin. (I tried to do that on XP, but ran into problems and went back to having my normal userid be the admin. Don't need a solution for that because I'm no longer running XP and besides it was my own fault.)
. My $HOME is /c/barry
I did that by adding the following to the .bashrc created when I installed cygwin:
mount C: /c
HOME=/c/barry
cd
. .bashrc
. The first two lines of the PHP script are:
#!/c/xampp/php/php
<?php
. Yes, the copy of PHP I want to use is in C:\xampp\php\php.exe
. I have another, similar script that I wrote on WinXP. It starts with the same two lines. It worked fine under XP, but doesn't work on Win7.
My best guesses at the source of the problem:
A difference between XP and Win7
I fumbled the home directory change in some way. IIRC, when I installed cygwin on XP I did some trickery so it would start directly in C:\barry but I couldn't remember how I did that when I installed it on Win7, so I took the "easy way out".
bash is passing to "C:\xampp\php\php.exe"
the file name "/c/barry/bin/toalpha"
but that is a cygwin path and your PHP does not regognize it.
You should convert it in an understandable one using
cygpath
Try
/c/xampp/php/php $(cygpath -w /c/barry/bin/toalpha )

Running shell script from webserver

I have a shell script named brew.sh as follows:
#!/bin/bash
brew doctor > test.txt
Which works fine when I run it from the command line. It outputs as expected the output of brew doctor to the test.txt file. However, when I call it from a php webpage as below it creates the test.txt file but the file is empty.
<?php
$output = shell_exec("./brew.sh");
echo $output;
?>
I suspected it was a permissions/ownership issue so I used chown to change ownership from username:staff to _www:_www on the parent directory,where it would then create the test.txt file but not write the output to it. I've even tried changing permissions to 777 temporarily on the test.txt and even that didn't work. Ultimately I would like to view the output of brew doctor and brew update on a webpage on my local development machine. Surely it shouldn't be that difficult?
Elias is going to be right I suspect - your environment on the cmd line is not going to be like the environment the script finds itself in within the webserver, assuming the webserver user is even allowed to run a proper shell...
You'll need a path. Check what your path value is, and put it in.
Also doesn't hurt to always include the full path for your binary, i.e. brew.
#!/bin/bash
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
/usr/local/bin/brew doctor > /var/www/whatever/test.txt
.... personally.. This isn't the approach I would use. If this is for brew doctor and brew update I would just use cron jobs that redirect output to a file on the webserver since I wouldn't want the webserver invoking brew commands... someone could DoS the server pretty easily just by requesting those pages a lot. The cron solution is pretty bulletproof though - and in any case with cron I can run it as a more appropriate user where the permissions/environment for brew are likely to work more correctly in any case.
Also - the other guys point about checking errors is a good one. stderr is probably ending up in your web logs at the moment when you get empty output. 2>&1 can make it go into that file instead, probably.
[ Note that it has to be 2>&1 !! I think chris had a typo when he wrote >2&1 which does something very different. 2 is the stderr filehandle which we want to redirect to 1. >2 will redirect 1 (stdout) to a file called 2.
If you add 2>&1 to your shell command, you should get STDERR and STDOUT returned which will at least help you determine what is going on:
<?php
$output = shell_exec("./brew.sh >2&1");
echo $output;
?>

Windows equivalent to #!/usr/bin/php

I am trying to execute a php file from sos-berlin jobscheduler. My OS is windows 7. I am following the documentation
http://www.sos-berlin.com/mediawiki/index.php/How_to_execute_a_PHP_script_with_JS
#!/usr/bin/php
<?php
echo "hello";
?>
But when I executed the script I am getting the error that
'#!' is not recognized as an internal or external command, operable program or batch file.
I came to understand that #!/usr/bin/php is a linux command. I should give here equivalent batch command. I googled, but didn't find any useful result. Please any one help me...
Thanks in advance.
Suppose you have installed php.exe in C:\Program files\MyPhpProgram\php.exe
Then run below 2 lines on terminal (cmd.exe):
assoc .php=phpfile
ftype phpfile="C:\Program files\MyPhpProgram\php.exe" "%1"
Go to your desktop.
Right click My Computer shortcut in the desktop.
Click Properties.
You should see a section of control Panel - Control Panel\System and Security\System.
Click Advanced System Settings on the Left menu.
Click Enviornment Variables towards the bottom of the window.
Select PATH in the user variables list.
Append your PHP Path (C:\myfolder\php) to your PATH variable, separated from the already existing string by a semi colon.
Click OK
Open your "cmd"
Type PATH, press enter
Make sure that you see your PHP folder among the list.
That should work.
install a php engine on windows
in a cmd windows for example, run php <path/to/your/script>
open cmd, go to where you installed php. and run c:\program files\php\php.exe <path/to/your/scirpt>.php
link to setting up the path to include php so you can run it easily from cmd:
http://willj.co/2012/10/run-wamp-php-windows-7-command-line/(dead link)

File not found, but files are present

I'm working on a server where users should be able to run protein sequences against a database, and it uses an executable called blastall. The server generates an executable which it should then run using batch. However, it doesn't appear to be running. Here is an example of an executable is generates (cmd.sh):
#!/usr/bin/env sh
cd /var/www/dbCAN
php -q /var/www/dbCAN/tools/blast.php -e -w /var/www/dbCAN/data/blast/20121019135548
Where the crazy number at the end of that is an auto-generated job ID based on when the job was submitted. There are 2 issues, and I'm trying to solve one at a time. The first issue is that when manually executed (by me simply running ./cmd.sh), I get the following errors:
sh: 1: /var/www/dbCAN/tools/blast/bin/blastall: not found
sh: 1: /var/www/dbCAN/tools/blast/bin/blastall: not found
sh: 1: -t: not found
But this doesn't really make sense to me, as the directory specified does in fact contain blastall. It has full rwx permissions and every directory along the way has appropriate permissions.
The blast.php file in tools looks like this:
try {
do_blast($opts["w"]);
$info['status'] = 'done';
$fp = fopen("$opts['w']/info.yaml","w")
fwrite($fp, Sypc::YAMLDump($info)); fclose($fp);
}
With of course variable declarations above it, and the do_blast function looks like this (again with variables declared above it and a cd so the directories work out):
function do_blast($workdir)
{
system("/var/www/dbCAN/tools/blast/bin/blastall -d data/blast/all.seq.fa -m 9 -p blastp -i $workdir/input.faa -o $workdir/output.txt")
system("/var/www/dbCAN/tools/blast/bin/blastall -d data/blast/all.seq.fa -p blastp -i $workdir/input.faa -o $workdir/output2.txt")
}
Any idea what may be causing this issue? I thought it may be because I'm running it and it was created by apache, but rwx is allowed for all users. I can include more information if needed, but I chose not to at this point because the original person who wrote the PHP split up everything into tons of little files, so it's difficult to pinpoint where the problem is exactly. Any ideas (if not complete solutions) are very appreciated.
EDIT: Solution found. As it turns out, the blastall executable had been compiled on a different linux system. Switched to a different executable and it ran flawlessly.
Could it be an issue with relative paths in your script? See my answer here, maybe it helps:
finding a file in php that is 4 directories up
The solution was to recompile the blastall executable. It had been compiled for Redhat and I am using Ubuntu. Unfortunately I assumed the executable I was given was for my system, not the previous one.

Categories