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?
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
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: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.