Printing program output to webpage as it is received
I honestly have no idea if this question has been asked before, or something similar like it. It's hard to search on such a thing.
So, here goes. I would like my webpage to run a program (PHP would be the correct choice here, yes?) hosted on the server, and then use the output of that program to print to a webpage. However, the program doesn't print its output all at once, thus, when I've tried this before, it waits until the program exits successfully before printing output, rather than printing it as it is received. (Which makes sense)
How would one go about printing output to the webpage as each new result is returned? That is, after displaying the webpa开发者_运维技巧ge, have entries be added to a div as the program outputs lines of text.
Ajax? Javascript? I'm just looking for what the be the correct way to go about this, not code.
Personally, I would do this with three files:
- The script being run on the server (worker.php)
- The php file being called by the client (index.php)
- A second php file to process ajax requests from index.php (ajax.php)
My workflow would be as follows:
- User calls index.php
- index.php makes a single call to worker.php, who starts producing output into a textfile (output.txt)
- index.php continually pings ajax.php seeing whether anything new has been added to output.txt, and continually updates as more output is available
- When worker.php is done, it outputs an EOF signal in the textfile, and deletes it X seconds later. (X is some value larger than the delay between pings to the ajax.php file.)
The basic idea is you open a pipe to the child process and read from it as if it were a file. For PHP in particular, take a look at proc_open.
精彩评论