Possible Duplicate:
Can PHP detect if its run from a cron job or from the command line?
I’m trying to determine whether or not the STDOUT resource on a PHP command-line script is being piped to another command or not, in order to appropriately display tabular data (if output is direct to terminal, it looks good to have table cells wrapped in +-----+ borders; but not if trying to inspect that data with awk or another command.)
Afte finding this answer, I’ve tried inspecting the STDOUT resource with stream_get_meta_data() and all of the other functions I can find in the PHP manual which operate on streams, but in every case, the resource looks exactly the same whether it is being piped to another process or not.
Is there any way to find this out in PHP?
You can use
posix_isattyassuming you have the POSIX extension (you probably do):If it’s true, then you are not outputting to a pipe.
isattyis a common method for doing this inCprograms and others.Note that this doesn’t check that it’s output to a pipe specifically, just whether it’s output to an interactive terminal (a pipe is not one).