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纯粹是障眼法而已, 必须指定为全局变量,否则文件描述符将在函数执行完毕之后被释放。

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值