Page 1 of 1

Read from STDIN only if data passed to STDIN

Posted: Thu Nov 01, 2007 10:06 am
by Ollie Saunders
I'd like to read from stdin, only if data is present. So that I can make...

Code: Select all

# php myscript.php
...do something different from...

Code: Select all

# cat somefile | php myscript.php
If I use file_get_contents('php://stdin') the shell starts taking input if nothing is piped. I don't seem to be able to use filesize() or anything like that to tell how much data there is without causing the shell to start taking input. fpeek() would probably do the trick, if it existed.

Re: Read from STDIN only if data passed to STDIN

Posted: Thu Nov 01, 2007 11:42 am
by Christopher
It seem like to you to do the test in the shell:

Code: Select all

if test something ;
then
     php myscript.php
else 
     cat somefile | php myscript.php
fi

Posted: Thu Nov 01, 2007 11:45 am
by Ollie Saunders
Hmm I think you misunderstand. I want myscript.php to be able to tell if something has been piped to it or not.

Posted: Thu Nov 01, 2007 12:55 pm
by Christopher
I looked at the $_SERVER vars and I don't see anything that would give you information where you could determine that. Maybe add an arg or set a shell var?