Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flush output after each `echo` call?

I have a php script that only produces logs to the client.
When I echo something, I want it to be transferred to client on-the-fly.
(Because while the script is processing, the page is blank)
I had already played around with ob_start() and ob_flush(), but they didn't work.

What's the best solution?

PS: it is a little dirty to put a flush at the end of the echo call...

EDIT: Neither the Answers worked, PHP or Apache Fault?

like image 317
cusspvz Avatar asked Jun 28 '10 14:06

cusspvz


People also ask

What is OB flush?

Definition and Usage. The ob_flush() function outputs the contents of the topmost output buffer and then clears the buffer of the contents. The output may be caught by another output buffer or, if there are no other output buffers, sent directly to the browser.

What is flush function?

In the C Programming Language, the fflush function writes any unwritten data in stream's buffer. If stream is a null pointer, the fflush function will flush all streams with unwritten data in the buffer.

What is the purpose of flush () in PHP?

Definition and Usage. The flush() function requests the server to send its currently buffered output to the browser. The server configuration may not always allow this to happen.

What does Ob_start () function do?

The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.


1 Answers

I've gotten the same issue and one of the posted example in the manual worked. A character set must be specified as one of the posters here already mentioned. http://www.php.net/manual/en/function.ob-flush.php#109314

header( 'Content-type: text/html; charset=utf-8' ); echo 'Begin ...<br />'; for( $i = 0 ; $i < 10 ; $i++ ) {     echo $i . '<br />';     flush();     ob_flush();     sleep(1); } echo 'End ...<br />'; 
like image 94
Teno Avatar answered Oct 25 '22 09:10

Teno