PHP - detect STDIO input

php, stdio

Solution

if(FALSE !== ftell(STDIN))
{
    while (FALSE !== ($line = fgets(STDIN)))
    {
        $customLogArr[]=$line;
    }
}

For STDIN, if nothing can be read, `ftell()` will return false.

Problem

What I want is being able to optionally pipe STDIO to a PHP script. If not, it'll take input from a file instead. So sometimes I'll simply run the script, other times I'll do something like ``` grep text logfile | php parseLog.php ``` I have a loop very much like this and it works fine when STDIO exists: ``` while (FALSE !== ($line = fgets(STDIN))) { $customLogArr[]=$line; } ``` When there's no STDIO though it halts waiting for some, it doesn't even enter the loop. What I'd like to do is be able to detect if I have STDIO input or not. Is there a way to do that?

Original source