Edit report at http://bugs.php.net/bug.php?id=34972&edit=1
ID: 34972 Comment by: dewi at dewimorgan dot com Reported by: VJTD3 at VJTD3 dot com Summary: STDIN won't allow nonblocking. Status: Assigned Type: Bug Package: Streams related Operating System: win32 only PHP Version: 5.2CVS-2008-07-15 Assigned To: pajoye Block user comment: N Private report: N New Comment: As I understand it (and some or all of this post may be wrong: please correct!), this is a platform limitation, since stuff like Windows' SetCommTimeouts() doesn't work for the stdin handle. I believe that this is because of the way pipes are handled under Windows, which makes them line-based rather than character-based. Any true "fix" (rather than just editing the docs) is a Hard Problem, since PHP has no control over Windows pipes. The windows PHP port could internally do something nasty like: if (ftell(STDIN) === 0) { // STDIN is the real STDIN, from a file or piped command. } else { // STDIN is a faked STDIN, built from keypresses. } This would fix the issue for those wanting interactive input, but the issue would still show up for pipes that aren't line separated: ReplayKeyboardInput.exe | myScript.php And even ignoring pipes, faking STDIN isn't trivial, either, but would probably involve a call to PeekConsoleInput(). I'm pretty sure if someone has some magic way for the receiving end of a pipe to make the pipe behave in a character-based way under Windows, then they'd happily implement it. I'm considerably less convinced that they'd be willing to muddy PHP with a STDIN kludge that wouldn't work in some cases. The NEED most people are expressing here is the need for interactive console input, via nonblocking keyboard reads, rather than the need for character-based pipes. So, a "more correct" option would likely to be getting the ncurses extension working under Windows - the one platform that really needs it, and doesn't have it :) The public domain pdcurses.sourceforge.net may be useful here. Workarounds: As a workaround for PHP programmers on windows in the meantime, an external program that reads a single keypress can be used. For people who don't mind blocking, this should work: $KeyPressed = `perl -e "use Term::ReadKey;ReadMode 'cbreak'; print(ReadKey(0)); ReadMode 'normal';"` To get nonblocking behaviour, where you can just read pending keystrokes, you'd need to launch that in a separate process, give it focus, and get it to send the characters to PHP whenever it read a character, and get your php file to poll the same file/socket for changes. If you don't have perl installed, a similar external program could likely be written in PowerShell, running as root and using $host.ui.rawui.KeyAvailable a WMI listener - but that's a crazy hoop to have to leap through. Unless you have a really pressing need to write your app in PHP, then, odds are, it's easier to just write your script in a language which has functions for reading the keyboard directly (Perl, PowerShell, Python, AutoHotKey...). Or write a php readkey extension that launches a keyboard monitor in a separate thread or something, with start, stop, and flush functions for that key buffer. Previous Comments: ------------------------------------------------------------------------ [2011-01-01 10:48:41] paj...@php.net @support at aculane dot com what makes you think that anything has been done about this feature request? ------------------------------------------------------------------------ [2011-01-01 10:33:12] support at aculane dot com PHP 5.3 (5.3.4) VC9 x86 Thread Safe (2010-Dec-09 23:14:27) windows 7(64bit os) Exact same problem. ------------------------------------------------------------------------ [2010-12-24 10:14:21] lgynove at 163 dot com 5 years after the bug should not be there. ------------------------------------------------------------------------ [2010-10-02 17:25:14] axellc dot perso at gmail dot com Have same problem with 5.3.3 on windows (7) I still can't use my php5-cli shell applications on Windows :s ------------------------------------------------------------------------ [2010-08-31 13:13:26] me at ben-xo dot com Experiencing this issue on Win 32 with PHP 5.3.2. stream_set_blocking(STDIN, 0) ('correctly') returns false and does not allow non-blocking. Also experiencing issues using stream_select() on STDIN. STDIN always comes back as ready for reading, but reading from it blocks. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=34972 -- Edit this bug report at http://bugs.php.net/bug.php?id=34972&edit=1