Page 1 of 1
Command line: Request input?
Posted: Sun Aug 06, 2006 6:15 pm
by TS_Death_Angel
Is there a way for a PHP script running from the command line to request user input via keyboard?
Ex. Please enter your age: [cursor]
Thanks.
Posted: Sun Aug 06, 2006 6:40 pm
by Ambush Commander
Several ways, enumerated in the PHP manual section on CLI scripting.
Personally, I prefer this on Windows systems:
Code: Select all
// set up streams
$GLOBALS['stdin'] = fopen('php://stdin', 'r');
// Sets up a prompt for command line scripts
function cli_prompt($length = 255) {
$line = fgets($GLOBALS['stdin'],$length);
return trim($line);
}
// ...
echo "Please enter your age: ";
$age = cli_prompt();
I believe there is a constant for stdin when you're on Unix.
Posted: Sun Aug 06, 2006 6:44 pm
by TS_Death_Angel
Cheers

You can't imagine how long I've been looking for the solution to this...

Thanks again.