执行“更少”从带有滚动功能的命令行 PHP
我想从 PHP 命令行执行 less
和类似的程序。
我已经尝试了常见的方法(exec、shell_exec、passthru 等),虽然其中许多可以将文件转储到屏幕上,但在我可以使用它之前该进程就终止了。如果我想要cat
,我会使用它。
我如何以这种方式执行程序?
I want to execute less
and similar programs from the command-line in PHP.
I have tried the usual suspects (exec, shell_exec, passthru, etc), and while many of them can dump the file to the screen, the process is terminated before I can make use of it. If I wanted cat
, I'd use it.
How do I execute a program in this fashion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 proc_open 通过管道向进程提供输入并从进程获取输出。然而,它似乎并不允许用户通过管道进行交互,因为它基本上降级为
cat
命令。这是我的第一个(失败的)方法:我想对于某些命令来说,这种方法可能实际上有效 - 并且在
proc_open
的 php 手册页中有一些示例可能有助于查看 - 但对于less
,你会得到整个文件,并且无法进行交互,可能是由于 Viper_Sb 的答案提到的原因。...但如果这就是您所需要的,那么模拟
less
似乎很容易。例如,您可以将命令的输出读取到行数组中,并以小块的形式提供:You could use
proc_open
to feed input to and get output back from a process via pipes. However, it doesn't seem like less allows for user interaction via pipes as it basically degrades to acat
command. Here's my first (failed) approach:I guess for some commands this approach might actually work - and there are some examples in the php manpage for
proc_open
that might be helpful to look over - but forless
, you get the whole file back and no possibility for interaction, maybe for reasons mentioned by Viper_Sb's answer....But it seems easy enough to simulate
less
if that's all you need. For example, you could read the output of the command into an array of lines and feed it in bite-sized chunks:我不相信这是可能的。 PHP 不是 VM/shell 环境,它访问其他程序的命令都将控制权返回给它,并且在 PHP 运行时通常没有交互。
最后一件事,尝试使用反引号运算符,如果这不起作用,那么我很确定如果不自己编写一些可以休眠并允许用户输入等的内容,您就无法做到这一点...默认情况下没有
I don't believe this is possible. PHP is not a VM/shell environment, the commands it has to access other programs all return control to it, and normally there is no interaction while PHP is running.
One last thing, try with the backtick operators, if that doesn't work then I'm pretty sure you can't do this without writing up something yourself that will sleep and allow user input etc... by default no
添加 exec('stty cbreak'); PHP 脚本也解决了这个问题。
我将以下内容放入由 php.ini 中的 auto_prepend_file 设置定义的文件中
,因此,我将 php.ini 编辑为以下内容:
然后在 /path/to/prepend.php 中,我将添加以下行:
我不太确定原因。我读过 PHP 的错误报告。但我不确定版本。我注意到以下设置存在问题:
但是,以下设置并未显示该问题:
值得注意的是,没有问题的版本使用的是 cPanel,另一个版本使用的是通过 yum 安装的默认 CentOS 6。
Adding exec('stty cbreak'); to the PHP script also fixes the issue.
I put the following in a file defined by the auto_prepend_file setting in php.ini
So, I would do something like edit php.ini to the following:
Then in, /path/to/prepend.php, I would add the following line:
I'm not exactly sure of the cause. I've read bug reports for PHP. I'm not sure about versions though. I noticed the problem with the following setup:
However, the following did not show the issue:
It is worth noting that the version without the issue was using cPanel and the other was using the default CentOS 6 install via yum.