1

I have the following code

<?php
       if(isset($_POST['cond']){
            echo '<br><p>Script to execute : xxxx.php<p>';
            echo "<p>Executing</p>";
            shell_exec('sleep 5');
            echo "<p>Done</p>";
       }

?>

Basically, the user clicks on a button that sends the 'cond' to the same page, which then enters the if block. I want to warn the user when the script is starting to execute, then execute the script, then warn the user that script has been executed; the current behavior is that the shell_exec('sleep 5')must be executed entirely before the echo statements are printed to the web page.

In fact, if you take the code inside the if block and put in a blank page and try to load it, you have to wait the 5 seconds of sleep before the page even gets loaded, then you see the echo statements. I don't understand this behavior, PHP isn't Javascript and each line of code should be executed sequentially?

All are executed on a Debian GNU / Linux 9 remote server

Thanks in advance

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
Benoit F
  • 479
  • 2
  • 10

1 Answers1

0

try this:

if(isset($_POST['cond'])){
    echo '<br><p>Script to execute : xxxx.php<p>';
    echo "<p>Executing</p>";
    if (ob_get_length()) {
        ob_end_flush();
        flush();
    }
    shell_exec('sleep 5');
    echo "<p>Done</p>";
}