Is there a command line php shell available for windows? I looking for something similar to the python shell, that will allow me to open and immediately begin executing code.
There is the windows command line for PHP: http://php.net/manual/en/install.windows.commandline.php
Try this:
<?php
$fh = fopen('php://stdin', 'r');
$cmd = '';
$bcLvl = 0;
while (true)
{
$line = rtrim(fgets($fh));
$bcLvl += substr_count($line, '{') - substr_count($line, '}');
$cmd.= $line;
if ($bcLvl > 0 or substr($cmd, -1) !== ';')
continue;
eval($cmd);
$cmd = '';
}
Save this code to a file (eg shell.php) and then run it from console:
php shell.php
Hit CTRL+C to exit.
Have a look at either running php.exe with the -a argument, or maybe the phpsh project.
Another simple variant, influenced by other answers. Create and run the following cmd-script:
#echo off
:loop
php.exe -r "while (true) { eval (fgets (STDIN) ); echo PHP_EOL; }"
goto loop
Immediate execution, Ctrl+C for exit. Insert correct path before "php.exe".
There is php-shell - it is not great, but still way better than php -a. (Also, it is dead simple to install, just run pear install http://jan.kneschke.de/assets/2007/2/17/PHP_Shell-0.3.1.tgz.) There is also phpa-norl, but I haven't tried it.
I found these two on github as well:
ubermuda/PHPInteractiveShell
https://github.com/ubermuda/PHPInteractiveShell
d11wtq/boris · non windows
https://github.com/d11wtq/boris
PsySH is the best REPL shell I have seen recently. Tab-completion, phpdoc support, proper namespace handling, interactive debugging, plugin support and many more. It is pure PHP and has composer support so it should be easy to install on Windows. I have only tried it on Linux but looking at the code, it seems to even have some history support on OSes without readline.
#Pavle Predic 's answer (https://stackoverflow.com/a/15742101/2752670) works for me on Windows.
But I want to use the shell.php file anywhere, so my solution is:
new and save shell.php in php installed dir (or where else you like), e.g. C:\Program Files\php-5.6.12-Win32-VC11-x64\shell.php
new a Windows environment variable, key: shell.php, value: "C:\Program Files\php-5.6.12-Win32-VC11-x64"
restart computer
use anywhere in the system:
php %shell.php%
Maybe answered - Read here: https://stackoverflow.com/a/19085620/2480481
I think i found easiest way to do it because im so interested to use myself :)
I try all options from here and from some other forums/pages... Windows cannot use readline.
Related
Thank you for taking the time to help me today. I have what I hope is a simple question. I have been attempting to use php exec() or any related PHP command to Open up the Developer Command Prompt for Visual Studio 2013 and use it to compile a file and save the output to a file on my local machine. I have it working fine from Run on Windows, but I can't seem to get it to work with PHP exec(). Here is how I have the command set up currently.
$cmd = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat && cd C:\wamp\www\csc424Try3\app\uploads && cl /EHsc basic.cpp && basic >> C:\wamp\www\csc424Try3\app\outputs\output.txt';
exec($cmd, $result);
As you can see, I am chaining together commands. The first command allows the prompt to open, the second changes to the proper directory, the third runs the command for compiling in the prompt, and the fourth outputs to a text file.
Any ideas where I'm going wrong? I had a feeling it had something to do with formatting, but perhaps chaining the commands together does not work in PHP the way it does on the system.
You're forgetting quotes around most of your arguments in the exec call's string, meaning that things like Program Files will be seen as two separate things, not a single directory.
$cmd = '"C:\Program Files (x86)\....VsDevCmd.bat" && etc...';
^-- ^-- need these
as is, you're trying to execute a program called C:\Program, with some extra arguments like Files (x86)\......
I found the answer to my own question after some research. The way I was trying to do it before just wasn't working. After talking to a colleague, I thought writing a .bat file for the background would work better, and it absolutely did. I wrote the chain of commands in a .bat file and executed it like follows;
system("cmd /c C:\wamp\www\csc424Try3\app\uploads\stuff.bat");
Success! The file can now be successfully compiled and ran from PHP. :) It is a good day.
I know this topis is there already, but none of the answers helped me... I copied wkhtml folder from my HDD onto the server. When I run
exec('"../wkhtmltopdf/wkhtmltopdf.exe" "www.estiroad.com/export.php" "C:/EXTREM.pdf"');
nothing happens... Do I type the paths correctly? I mean, I need to type exact path to the wkhtmltopdf according to where I run the exec command from, right? And if I want to save it onto users HDD, I need to use absolute path, right? Strange is, that it gives me no error, just silently does nothing... I found about PHP bindings, but I don´t understand how to use them... Everybody solves that problem only in Linux and thats worthless for me :( Any help will be appreciated.
You should't be putting the quotes around the library.You can catch the output of the command this way:
$commandString = '../wkhtmltopdf/wkhtmltopdf-i386 http://www.estiroad.com/export.php file.pdf 2>&1';
$output = shell_exec($commandString);
The 2>&1 in UNIX will mean that the output will come through. 1 is stdout. 2 is stderr.
Hope this helps.
Or in windows
$commandString = '../wkhtmltopdf/wkhtmltopdf.exe http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));
From the permission issue it looks like you're running the production script on linux. Go to your production server and run
$ uname -a
You'll get something like:
Linux ora100 2.6.5-7.252-smp #1 SMP Tue Feb 14 11:11:04 UTC 2006 x86_64 x86_64 x86_64 GNU/Linux
the x86_64 suggest you're running a 64 bit CPU, if that's the case download the amd64 version of binary, otherwise download the i386 one. Both can be obrained from this url: http://code.google.com/p/wkhtmltopdf/downloads/list
Keep the windows binary. Have you got a config file? if you do make sure you have a switch where you assign your library path to a constant based on your environment.
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// this is windows server
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf.exe");
} else {
// or the 64 bit binary?
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-i386");
}
Then change your code that initiates wkhtmltopdf:
$commandString = WKHTML_LIB' http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));
Use snappy from knplabs. Its a wrapper around wkhtmltopdf and handles the cases.
(I have experience with wkhtmltopdf, but ONLY on *nix)
My advice: First do EVERYTHING without PHP, no exec().
Simply make sure you can type on the commandline the command to create the PDF.
Are you sure you installed WebKit on the machine? wkhtmltopdf.exe depends on it.
Only after you are sure you can generate from commandline, try to translate your action to PHP. ALso make sure PHP has appropriate rights to execute wkhtmltopdf.exe, AND has appropriate right to write away to C:/EXTREM.pdf.
Is there a PHP syntax checker plugin for Notepad++?
Please don't answer "Use another editor instead"
Try NppExec plugin for Notepad++. Using it create a command to be something like this:
cmd.exe /K c:\your\path\to\php.exe -l "YOUR_FULL_FILE_NAME"
Instead of YOUR_FULL_FILE_NAME you should use appropriate Notepadd++ macro -- I think it is $(FULL_CURRENT_PATH) but double check with NppExec manual (installs together with plugin).
P.S.
But any IDE will be better for sure (I'm using PhpStorm). If IDE is too heavy for your PC then look for php-oriented editors, like Blumentals RapidPHP etc (it's lighter than full IDE but may have all really important features).
As LazyOne said above, you can use NppExec which you can install using the plugin manager (Plugins>Plugin Manager>Show Plugin Manager) You'll also need to have PHP installed. Lastly, the command I use to do PHP syntax checking with NppExec is
"C:\Program Files (x86)\PHP\php.exe" -l $(FULL_CURRENT_PATH)
I recommend you find a true IDE (not a glorified text editor). I've used Notepad++ for years but it can't do much beyond syntax highlighting.
I personally use PHPStorm (but it's not free, it is very good though :D ). You could also use NetBeans or Eclipse.
Adding to #LazyOne's answer:
I don't like NetBeans, it's too strict, has a tough time finding includes, and it's slow. I dig N++ for its speed and simplicity. I have php installed on my PC really just to run validation. If you're using N++ (or any other text editor) you can use the following Powershell script to batch check all of the files you've downloaded and are working on. Just fire up Powershell ISE, enter the correct path to check and PHP.exe path for your environment and the results get output to the ISE console.
cls
$pathToCheck = "C:\Users\BigDaddy\AppData\Local\Temp\fz3temp-1"
$phpExePath = "C:\PHP\php.exe"
Get-ChildItem $pathToCheck -Filter "*.php" | foreach {
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $phpExePath
$pinfo.Arguments = "-l", $_.FullName
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$output = $p.StandardOutput.ReadToEnd()
$output += $p.StandardError.ReadToEnd()
$output
}
I hope someone else finds this as useful as I have.
Cheers!
I use Komodo Edit 7 (free version) which has a built-in php syntax checker. I dunno how robust it is, but it works fine for me. I’m not a pro web designer, but I like it better then Eclipse and Bluefish. Komodo is smaller than Eclipse and more stable than Bluefish (in my Win XP environment).
PHP can syntax check your file using the -l lint option. Install PHP (if you haven't already) on your computer and use the Run function in Notepad++ and run a command like this:
cmd.exe /S /K ""C:\Program (portable)\php-8.0.12-Win32-vs16-x64\php.exe" -l "$(FULL_CURRENT_PATH)""
Change the path to your install location. After running it via Run you are able to save it, give it a name and assign a custom keyboard shortcut.
Breakdown of the command:
cmd.exe /S /K open a new Command shell which will stay open after executing, /S for being able to wrap the whole command to execute in cmd.exe in quotes
"C:\Program (portable)\php-8.0.12-Win32-vs16-x64\php.exe" launch php.exe
-l option of php.exe to use their lint service rather than executing
"$(FULL_CURRENT_PATH)" Notepad++ specific which gives the full path to the current open document
I have a problem trying to run passthru function in my php code (Joomla module). the code is following (this is only a snippet)
ob_start();
passthru("/usr/bin/whois 85.70.231.130 | /usr/bin/grep 'address:'",$code);
$whoisData = ob_get_contents();
ob_end_clean();
$whoisData = str_replace("address:", "", $whoisData);
$whoisArray = split("\n",$whoisData);
echo trim($whoisArray[1]);
when I run this on my localhost, it echoes what it should, but when I run this code on the production server, it echoes nothing and the $code variable contains 127 (command not found). I tryied add absolute paths to these commands into the passthru function, but it didn't helped. Interesting is, that when I run the code right from terminal via ssh and php command, it runs well, but when it's called from application context it doesn't. Does anybody know what I should to do?thanks
SOME EDITS..
safe_mode is on
webserver does not see into /usr/bin and /bin/ folders so what is the best way how to run these commands from php?
usr/bin/grep doesn't look like a valid path to a command.
The missing / at the beginning of the path to the second command might explain the command not found error... even if the first whois command is found.
Have you looked to see if your webserver / php is running chrooted?
print_r(glob('/*'));
if (file_exists('/usr/bin/grep') && file_exists('/usr/bin/whois')) {
print "maybe its a permissions thing?\n";
} else {
print "can't see executables required\n";
}
should give you a clue.
So I have already solved my problem with phpwhois library. I seems like with my server configuration is it unlikely that these functions will be working well. So thanks for your help:)
Specifically I have a PHP command-line script that at a certain point requires input from the user. I would like to be able to execute an external editor (such as vi), and wait for the editor to finish execution before resuming the script.
My basic idea was to use a temporary file to do the editing in, and to retrieve the contents of the file afterwards. Something along the lines of:
$filename = '/tmp/script_' . time() . '.tmp';
get_user_input ($filename);
$input = file_get_contents ($filename);
unlink ($filename);
I suspect that this isn't possible from a PHP command-line script, however I'm hoping that there's some sort of shell scripting trick that can be employed to achieve the same effect.
Suggestions for how this can be achieved in other scripting languages are also more than welcome.
You can redirect the editor's output to the terminal:
system("vim > `tty`");
I just tried this and it works fine in windows, so you can probably replicate with vi or whatever app you want on Linux.
The key is that exec() hangs the php process while notepad (in this case) is running.
<?php
exec('notepad c:\test');
echo file_get_contents('c:\test');
?>
$ php -r test.php
Edit: As your attempt shows and bstark pointed out, my notepad test fires up a new window so all is fine, but any editor that runs in console mode fails because it has no terminal to attach to.
That being said, I tried on a Linux box with exec('nano test'); echo file_get_contents('test'); and it doesn't fail as badly as vi, it just runs without displaying anything. I could type some stuff, press "ctrl-X, y" to close and save the file, and then the php script continued and displayed what I had written. Anyway.. I found the proper solution, so new answer coming in.
I don't know if it's at all possible to connect vi to the terminal php is running on, but the quick and easy solution is not to use a screen editor on the same terminal.
You can either use a line editor such as ed (you probably don't want that) or open a new window, like system("xterm -e vi") (replace xterm with the name of your terminal app).
Edited to add: In perl, system("vi") just works, because perl doesn't do the kind of fancy pipelining/buffering php does.
So it seems your idea of writing a file lead us to try crazy things while there is an easy solution :)
<?php
$out = fopen('php://stdout', 'w+');
$in = fopen('php://stdin', 'r+');
fwrite($out, "foo?\n");
$var = fread($in, 1024);
echo strtoupper($var);
The fread() call will hang the php process until it receives something (1024 bytes or end of line I think), producing this :
$ php test.php
foo?
bar <= my input
BAR
system('vi');
http://www.php.net/system