phpapacheflushfpm

Question regarding ob_flush and flush not working on FPM


I have below code and want to show echo one by one but whole result returns at once


<?php 



for ($i = 1; $i <= 10; $i++){

  

  sleep(1); 

  echo "$i\n";

  

  ob_flush();

    flush();

  

}

It is ok in CGI but not in FPM (PHP 8.3 - apache 2.4)

I have tried several things to solve this problem, but none of these solutions worked. Here are some of them:

ini_set('output_buffering','Off');
ini_set('zlib.output_compression',0);
ini_set('implicit_flush',1);
ob_end_clean();
set_time_limit(0);
ob_implicit_flush(true);
ob_end_flush();
ob_start();

Any solution?


Solution

  • If you want to continue with FPM, you need ob_end_clean before first echo but with enough count, so the final status may be like this

    
    <?php
    
    while (ob_get_level()) ob_end_clean();
    
    for ($i = 1; $i <= 10; $i++){
    
      sleep(1); 
      echo "$i\n";
    
      ob_flush();
      flush();
    
    }