NYCPHP Meetup

NYPHP.org

[nycphp-talk] determining empty STDIN | php://stdin (cli)

Analysis & Solutions danielc at analysisandsolutions.com
Mon Sep 22 22:54:53 EDT 2003


Hi Jon:

On Mon, Sep 22, 2003 at 01:33:42PM -0400, jon baer wrote:
> normally the small scripts i write either getopt for it's content or read
> STDIN along the pipe, but i just tried to throw in a usage() function for
> null input and could not determine if STDIN was empty or not it, just hangs
> (W32).
> 
> ie:
> $d = fread(STDIN, 1024);
> 
> nothing seems to be able to determine if there is content ... any ideas?
> have not tried on debian yet.  thought checking (STDIN) would work but
> didn't.

I'm confused as to what you're really after.  Wouldn't a usage example 
apply to tips on passing arguments to a command line script?  Arguments 
for such scripts are found in $_SERVER['argv'], not STDIN.

As far as STDIN goes, I'm used to reading it via fopen() then fgets().

So, a quick and dirty example...


if ( empty($_SERVER['argv'][1]) ) {
    $_SERVER['argv'][1] = '';
}

switch ($_SERVER['argv'][1]) {
    case 'blah':
        $In = fopen('php://stdin', 'r');

        while ( !feof($In) ) {
           // do blah
        }
        break;

    default:
        echo 'usage...';
}

Enjoy,

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409



More information about the talk mailing list