Question

I'm trying to implement a simple program where some data needs to be edited in an external editor. Right now, the structure is about this:

$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...

Now the problem is that stdin/stdout don't get mapped correctly, causing vim to complain that stdout is not a terminal.

How can I call vim (or any other editor) in a way that the terminal gets connected correctly?

Était-ce utile?

La solution

A possible solution is to use proc_open (Documentation) instead:

$pipes = array();

$editRes = proc_open(
    "$editor $tmpfile",
    array(
        0 => STDIN,
        1 => STDOUT,
        2 => STDERR
    ),
    $pipes
);

proc_close($editRes);

Error handling omitted for brevity.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top