2

I am uploading a file via php move_uploaded_file function and then the file is then taken as a parameter of a java program. while the java program is executing, it outputs the progress in the terminal. The output of the java program is input of a perl script which does the same. I am executing all of them in my php web site as shell_exec functions and printing them on screen

    echo "Running java ..... ";
    $output=shell_exec("java -jar abc.jar ".( $_FILES['uploaded']['name']));
    echo "<pre>$output</pre>";
    echo "running perl .....";
    $output=shell_exec("perl abc.pl outputfromjava.txt");
    echo "<pre>$output</pre>";

The shell commands are running fine but the output from java and perl isn't displayed on my web site. I tried refreshing the page in my code after java and perl is executed but it then refreshes the upload too.

The output is now

Running java ....
Running Perl ....

How should I write my php so that the output is

Running Java...
Progress1 //from java output
progress2 //from java output
Running Perl....
Progress1 //from perl output
progress2 //from perl output

Thanks

3 Answers 3

4

Disable output buffering using ob_end_flush, and start the process using proc_open instead of shell_exec, so you have full control over the file descriptors and don't need to wait for the process to finish to get the output. When you have started the process using proc_open, read from its STDOUT and STDERR and output it to the browser, possibly with a flush to assure your output is not kept in any buffer.

Sign up to request clarification or add additional context in comments.

2 Comments

I used system instead of shell_exec and it worked. Does proc_open have any extra advantage over system?
definitely! you basically get full control over the process and its fd's (input/output). That is, your script keeps continues (after proc_open) while the command is running, giving a lot of potential benefits.
2

this is my solution: (tested on Windows machine + wamp server)

header('Content-Encoding: none;');

set_time_limit(0);

if (ob_get_level() == 0) {
    ob_start();
}

$handle = popen("<<< Your Shell Command >>>", "r");

while(!feof($handle)) {
    $buffer = trim(htmlspecialchars(fgets($handle)));
    echo $buffer . "<br />" . str_pad('', 4096);    
    ob_flush();
    flush();
    sleep(1);
}
pclose($handle);
ob_end_flush();

1 Comment

i have tried ur code,,actually its not displaying shell command in real time, but just wait shell command to finish load all text and displaying it bit by bit using sleep(1) help. sorry, u deserve downvote
-1

try > file.txt then the read file.

:)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.