Jump to content


Photo

output buffer control


  • Please log in to reply
9 replies to this topic

#1 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 30 August 2006 - 05:05 AM

Hello,

I need to be able to run some system commands and scripts which will have a lot of output. I would like to be able to not just it all dump to the screen in a long scrolling list. Is there some way to control the output and have it do things like when it reaches the max allowed lines, it just starts shifting up, erasing the top?

I'm not afraid of reading, but I'm not sure where to start. Can someone point me in the right direction?

Thanks!
...drkstr

PS: I would also like to build a "shell consol" form that simulates a system shell in the browser. This will not happen until further down the road, but if anyone has any ideas about how to attack this problem, I would really like to hear them. Thanks again!

#2 Satria Ox41464b

Satria Ox41464b
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 30 August 2006 - 05:24 AM

have you check http://php.net/outcontrol ?

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 30 August 2006 - 09:02 AM

Use shell_exec() instead of system(), shell_exec() returns all output as a string instead of directly outputting.

#4 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 30 August 2006 - 02:13 PM

have you check http://php.net/outcontrol ?

No I didn't. Thanks, that looks like what I was looking for.

Use shell_exec() instead of system(), shell_exec() returns all output as a string instead of directly outputting.

Awesome, thanks for the tip.

So if I understand you two correctly, I can start the output buffer with ob_start(), read the output to the command in as a string, explode it into an array of lines by prex_split'ing at "/\n/", then if count > x amount of lines, array_shift the first element off the array, then implode it back to a string and print it to the output buffer?

Thanks for the help everyone,
...drkstr

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 30 August 2006 - 03:47 PM

no need for preg_split, just follow the below example:
<?php

$output = explode("\n", shell_exec($command));

foreach ($output as $line) {
    echo '<p>' . $line . '</p>';
}

?>


#6 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 31 August 2006 - 01:23 AM

I don't know if that will do it for me, but I could be wrong. I need to have a continual stream open for the output. It's not a command that will just run once and send some output, I need to be able to display the output in real time while the process runs. Is this even possible?

Thanks for the help!
...drkstr

#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 August 2006 - 01:32 AM

It's not a command that will just run once and send some output, I need to be able to display the output in real time while the process runs. Is this even possible?


If your talking about attempting this within a server / browser environment then the simple answer is no. You could maybe take it on using some javascript and xhttprequest trickery, but that opens a whole new can of worms (Ajax).

The thing is, even using shell_exec() the string will not be handed to php until the command is finished proccessing. You would have to fake the real time appearence and I really dont think thats what you want, nor do I see the point.

#8 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 31 August 2006 - 03:28 AM

I was afraid of this :(

Unfortunately, it must be done one way or another. I will have to look into Ajax.

Thanks for the tip!
...drkstr

#9 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 31 August 2006 - 07:54 AM

AJAX won't help you anymore than PHP. Why? AJAX does not execute anything on the server.

#10 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 31 August 2006 - 05:45 PM

Well from what I have read so far, I think I might be able to do it this way. (keep in mind I'm still just tossing around ideas)

I can write a shell script which starts a command and dumps the output as raw data to a file (well call it output.txt). It would over write any previous data, and delete the file when there is no more output. The PHP will run the shell script in the background, pause for a second or two, then start a while( file_exists("output.txt") ) loop which reads in the contents of the file, wraps XML tags around it, and makes it available as an XML object to the AJAX client script. The client side would then grab an update of the XML object being hosted on a predefined interval depending on the available bandwidth.

Does this sound doable at all? Maybe there is an easier way?

Thanks,
...drkstr




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users