Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape a PHP script to an external editor and return afterwards?

Specifically I have a PHP command-line script that at a certain point requires input from the user. I would like to be able to execute an external editor (such as vi), and wait for the editor to finish execution before resuming the script.

My basic idea was to use a temporary file to do the editing in, and to retrieve the contents of the file afterwards. Something along the lines of:

$filename = '/tmp/script_' . time() . '.tmp';

get_user_input ($filename);

$input = file_get_contents ($filename);
unlink ($filename);

I suspect that this isn't possible from a PHP command-line script, however I'm hoping that there's some sort of shell scripting trick that can be employed to achieve the same effect.

Suggestions for how this can be achieved in other scripting languages are also more than welcome.

like image 968
Mathew Byrne Avatar asked Sep 24 '08 08:09

Mathew Byrne


1 Answers

You can redirect the editor's output to the terminal:

system("vim > `tty`");
like image 71
Ole Helgesen Avatar answered Sep 18 '22 22:09

Ole Helgesen