How can I detect STDOUT redirection in PHP CLI?
I've a PHP CLI script that uses shell escape sequences for bolding, but I want to be able to disable these automatically when the script's 开发者_C百科being redirected (eg to a log file).
I can find ways to detect STDOUT redirection in everything but PHP so far... so can anyone tell me how it is done in PHP?
This should give you what you want:
if(posix_isatty(STDOUT))
echo "No Redirection";
else
echo "Redirection!";
For Windows, you can use this:
$b = stream_isatty(STDIN);
if ($b) {
echo "no redirect\n";
} else {
echo "redirect\n";
}
https://php.net/function.stream-isatty
精彩评论