PHP中STDIN, STDOUT, STDERR的重定向

<?php
function test(){
    $file = '/tmp/std.log';
    global $STDOUT, $STDERR;
    $handle = fopen($file, "a");
    if ($handle) {
        unset($handle);
        @fclose(STDOUT);
        @fclose(STDERR);
        $STDOUT = fopen($file, "a");
        $STDERR = fopen($file, "a");
    } else {
        throw new Exception('can not open stdoutFile ' . $file);
    }
}

test();
print_r($_SERVER);

 命令行下运行以上程序,将不会在控制台输出任何内容,输出内容将被重定向到批定的文件/tmp/std.log中,我第一次看到这个代码时,非常诧异,百思不得其解。

 

$STDOUT, $STDERR 这并不是内置的变量, 只是普通的一个变量名称而已。为什么通过这样处理,就能实现输出重定向呢?

 

万能的stackoverflow给出了答案:

 

 

http://stackoverflow.com/questions/937627/how-to-redirect-stdout-to-a-file-in-php

<?php
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
$STDIN = fopen('/dev/null', 'r');
$STDOUT = fopen('application.log', 'wb');
$STDERR = fopen('error.log', 'wb');

 because when you close the standard input, output and error file descriptors, the first three new descriptors will become the NEW standard input, output and error file descriptors.

 

In my example here I redirected standard input to /dev/null and the output and error file descriptors to log files. This is common practice when making a daemon script in PHP.

解释说:

如果你关闭了标准输出,标准错误输出文件描述符,那么你打开的前三个文件描述符将成为新的标准输入、输出、错误的描述符。

 

使用$STDIN, $STDOUT纯粹是障眼法而已, 必须指定为全局变量,否则文件描述符将在函数执行完毕之后被释放。

 

利用这个特性,后台脚本运行时,就可以直接输出并被记录到相关的文件中,给我们跟踪程序带来极大的方便。

猜你喜欢

转载自zhangxugg-163-com.iteye.com/blog/2370126