I have a script that is designed to be run both as a web page, and via the console.
Detecting which method was used to invoke the script seems pretty straight forward, but when the script is being run from the console, I need to know if the script is being run interactively or not (user typing commands, or input redirected from a file).
php script.php
versus
php script.php < input_file
Is this possible?
I also needed a slightly more flexible solution than
posix_isattythat could detect:After a bit of experimenting and digging around through libc headers, I came up with a very simple class that can do all of the above and more.
Some example usage, to show what it can detect.
Input:
Output:
Error:
I’ve not included examples for links, sockets, or block devices, but there’s no reason they shouldn’t work, as the device mode masks for them are in the class.
(Not tested on Windows – mileage may vary)