我试图实现一个简单的程序,其中一些数据需要在外部编辑器中编辑。现在,结构是这样的:
$tmpfile = tempnam('foo', 'bar');
file_put_contents($tmpfile, $my_data);
$editor = getenv('EDITOR');
if (!$editor) {
$editor = 'vim';
}
system("$editor $tmpfile");
// read the modified file's content back into my data...
现在的问题是stdin/stdout没有得到正确的映射,导致vim抱怨stdout不是终端。
我如何调用vim(或任何其他编辑器)在一种方式,终端得到正确连接?
一个可能的解决方案是使用proc_open
(Documentation)代替:
$pipes = array();
$editRes = proc_open(
"$editor $tmpfile",
array(
0 => STDIN,
1 => STDOUT,
2 => STDERR
),
$pipes
);
proc_close($editRes);
为简洁而省略错误处理。