Re: Detecting Redirected Output [message #174123 is a reply to message #174114] |
Sun, 22 May 2011 16:17 |
Bob Smith
Messages: 11 Registered: October 2010
Karma:
|
Junior Member |
|
|
On 5/22/2011 7:06 AM, Thomas 'PointedEars' Lahn wrote:
> Bob Smith wrote:
>
>> Running under Win7, a script I'm writing needs to vary its behavior
>> depending upon whether or not the output from PHP is being redirected.
>> That is, I want to know from within the PHP script whether it's called as
>>
>> php foo.php
>>
>> or
>>
>> php foo.php> outfile
>>
>> None of the Output Control Functions appears to be helpful.
>>
>> Way back in DOS days, there was an IOCTL call which the program could
>> use to distinguish those two cases. Is there anything comparable in PHP?
>
> In PHP, yes; but not on Windows, as the POSIX extension is not available
> there (according to the manual):
>
> <http://lmgtfy.com/?q=php+cli+redirection+-http>
Having just learned that this is trivial in Python, I looked more
carefully at PHP on Windows and found a couple of ways to tell if STDOUT
has been redirected:
1. $fd = fopen ('php://stdout', "w");
if (ftell ($fd) === false)
echo "STDOUT not redirected.\n";
else
echo "STDOUT redirected.\n";
fclose ($fd);
2. $fd = fopen ('php://stdout', "w");
$a = fstat ($fd);
if ($a['dev'] != 0) // or ['rdev']
echo "STDOUT not redirected.\n";
else
echo "STDOUT redirected.\n";
fclose ($fd);
There also is a difference in $a['mode'] which I did not pursue.
--
_________________________________________
Bob Smith -- bsmith(at)sudleydeplacespam(dot)com
To reply to me directly, delete "despam".
|
|
|