如何将 PHP 脚本转义到外部编辑器并随后返回?
具体来说,我有一个 PHP 命令行脚本,在某个时刻需要用户输入。 我希望能够执行外部编辑器(例如 vi),并等待编辑器完成执行后再恢复脚本。
我的基本想法是使用临时文件进行编辑,然后检索文件的内容。 大概是这样的:
$filename = '/tmp/script_' . time() . '.tmp';
get_user_input ($filename);
$input = file_get_contents ($filename);
unlink ($filename);
我怀疑这在 PHP 命令行脚本中是不可能的,但是我希望有某种 shell 脚本技巧可以用来实现相同的效果。
关于如何在其他脚本语言中实现这一点的建议也非常受欢迎。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所以看来你写一个文件的想法导致我们尝试疯狂的事情,同时有一个简单的解决方案:)
fread() 调用将挂起 php 进程,直到它收到一些东西(我认为是 1024 字节或行尾),产生这个:
So it seems your idea of writing a file lead us to try crazy things while there is an easy solution :)
The fread() call will hang the php process until it receives something (1024 bytes or end of line I think), producing this :
http://www.php.net/system
http://www.php.net/system
您可以将编辑器的输出重定向到终端:
You can redirect the editor's output to the terminal:
我刚刚尝试过,它在 Windows 中运行良好,因此您可以在 Linux 上使用 vi 或任何您想要的应用程序进行复制。
关键是
exec()
在记事本(在本例中)运行时挂起 php 进程。编辑:正如您的尝试所示和 bstark 指出的那样,我的记事本测试会启动一个新窗口,因此一切都很好,但任何在控制台模式下运行的编辑器都会失败,因为它没有可附加的终端。
话虽这么说,我在 Linux 机器上尝试了 exec('nano test'); echo file_get_contents('test'); 并且它不会像 vi 那样失败,它只是运行而不显示任何内容。 我可以输入一些内容,按“ctrl-X,y”关闭并保存文件,然后 php 脚本继续并显示我编写的内容。 无论如何..我找到了正确的解决方案,所以新的答案即将到来。
I just tried this and it works fine in windows, so you can probably replicate with vi or whatever app you want on Linux.
The key is that
exec()
hangs the php process while notepad (in this case) is running.Edit: As your attempt shows and bstark pointed out, my notepad test fires up a new window so all is fine, but any editor that runs in console mode fails because it has no terminal to attach to.
That being said, I tried on a Linux box with
exec('nano test'); echo file_get_contents('test');
and it doesn't fail as badly as vi, it just runs without displaying anything. I could type some stuff, press "ctrl-X, y" to close and save the file, and then the php script continued and displayed what I had written. Anyway.. I found the proper solution, so new answer coming in.我不知道是否可以将 vi 连接到运行 php 的终端,但快速且简单的解决方案是不在同一终端上使用屏幕编辑器。
您可以使用行编辑器,例如 ed (您可能不想要这样),也可以打开一个新窗口,例如 system("xterm -e vi") (将 xterm 替换为您的终端应用程序的名称)。
编辑添加:在 perl 中,system("vi") 可以正常工作,因为 perl 不执行 php 所做的那种花哨的管道/缓冲操作。
I don't know if it's at all possible to connect vi to the terminal php is running on, but the quick and easy solution is not to use a screen editor on the same terminal.
You can either use a line editor such as ed (you probably don't want that) or open a new window, like system("xterm -e vi") (replace xterm with the name of your terminal app).
Edited to add: In perl, system("vi") just works, because perl doesn't do the kind of fancy pipelining/buffering php does.