You are here: nonblocking STDIN in CLI PHP app « PHP Programming Language « IT news, forums, messages
nonblocking STDIN in CLI PHP app

Posted by yawnmoth on 05/31/07 01:53

For learning purposes, I'd like to create a PHP script that'll output
the system time every minute and, at the same time, will process
"command line" commands as I type them into STDIN. eg.

If I wait a minute, it should output the time. If, ten seconds later,
I type in "dir", it should display the directory information.
Unfortunately, it doesn't do that.

Here's my code:

<?php
$stdin = fopen('php://stdin', 'r');

stream_set_blocking($stdin, 0);

$data = '';
$last_date = '';
while (true)
{
$data.= fgets($stdin, 1024);
if ($data{strlen($data)-1} == "\r" || $data{strlen($data)-1} == "\n")
{
system(trim($data));
$data = '';
}
if ($last_date != date('g:ia'))
{
$last_date = date('g:ia');
echo "\r\nThe time is now [$last_date]\r\n";
}
}
?>

Any ideas?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация