0

I've implemented a file optimization script which takes about 30 seconds or more. There's a loop in which I added echos to track what's being processed.

However, most of the time, no output is being sent, until the end of the process.

How can I control this in order to send the echos in an iteration just as they're being finished?

EDIT:

This is the implied code, with the output buffer functions:

    set_time_limit(60);
    ini_set("memory_limit", "256M");

    $documents = $this->documents_model->get($date1, $date2);

    ob_start();

    echo '-- Start ' . "<br>\n";

    ob_end_flush();
    flush();

    foreach ($documents as $document) {

        ob_start();

        echo '-- Processing document ' . $document->id . "<br>\n";

        $file = $document->get_file_name();
        if (! $file || ! file_exists(DOCUMENT_ROOT . 'documents/' . $file)) {

            echo '---- Document ' . $document->id . " has no PDF file yet or it was deleted<br>\n";

            $path = $this->documents_model->generatePDF($document);
            echo '------ file generated: ' . $path;
        }

        ob_end_flush();
        flush();
    }

    echo '-- End ' . "<br>\n";
luis.ap.uyen
  • 1,314
  • 1
  • 11
  • 29

1 Answers1

1

The reason why you will get the output after the processing finishes, is php's output buffer and eventually your webserver's buffer.

You will have to flush these buffers with the invokation of ob_flush() and flush() after every few echo statements.

For more technical information see PHP buffer ob_flush() vs. flush()

Community
  • 1
  • 1
l-x
  • 1,521
  • 9
  • 17
  • It's not working either with ob_flush() or flush(), or their combination. All the output is displayed at the end of the processing. – luis.ap.uyen Apr 01 '16 at 13:20