how to redirect STDOUT to a file in PHP?

前端 未结 7 1038
不思量自难忘°
不思量自难忘° 2020-11-27 11:56

The code below almost works, but it\'s not what I really meant:

ob_start();
echo \'xxx\';
$contents = ob_get_contents();
ob_end_clean();
file_put_contents($f         


        
7条回答
  •  迷失自我
    2020-11-27 12:01

    You can install Eio extension

    pecl install eio

    and duplicate a file descriptor

    $temp=fopen('/tmp/my_stdout','a');
    $my_data='my something';
    $foo=eio_dup2($temp,STDOUT,EIO_PRI_MAX,function($data,$esult,$request){
        var_dump($data,$esult,$request);
        var_dump(eio_get_last_error($request));
    },$my_data);
    eio_event_loop();
    echo "something to stdout\n";
    fclose($temp);
    

    this creates new file descriptor and rewrites target stream of STDOUT

    this can be done with STDERR as well

    and constants STD[OUT|ERR] are still usable

提交回复
热议问题