My basic environment is nginx1.6.2+php5.3.
To do a line-by-row output, the use of Ob_flush to try the N method does not work, such as the following code:
<?php
Ob_start ();
for (;;)
{
echo "<BR> ...";
Ob_flush ();
Flush ();
Sleep (1);
}
?>
Google a lot of writing is not good, so the problem should be in the environmental configuration rather than the use of methods.
In other words, stackoverflow to power, search "PHP flush not working" found a correct solution:
Check the Nginx configuration file (nginx.conf) to disable buffering for Nginx:
Proxy_buffering off;
gzip off;
Fastcgi_keep_conn on;
Pay attention to the last sentence of the fastcgi Oh ~ ~
Check php.ini, disable buffering:
Output_buffering = Off
Note that this configuration cannot be set dynamically in the program through the Ini_set () function, as explained in the official PHP manual:
The output_buffering setting is Php_ini_perdir therefore it could not being set using Ini_set ()
After the two-step configuration (nginx.conf and php.ini), reboot the Nginx, test the code at the beginning of the article, and successfully output line by row
Add: The difference between PHP flush () and Ob_flush ()
Buffer----Flush ()
Buffer is a memory address space, the Linux system default size is generally 4096 (1KB), that is, a memory page. It is primarily used for data transfer between devices that are not synchronized at the storage speed or between devices with different priority levels. By using buffer, the process can wait for each other to get smaller. Here's a popular example, when you open a text editor to edit a file, you input a character, the operating system will not immediately write this character directly to the disk, but first write to the buffer, when the full of a buffer, the data in the buffer will be written to disk, Of course, when calling kernel function flush (), it is mandatory to write the dirty data in the buffer back to disk.
Similarly, when executing echo,print, the output is not immediately transmitted via TCP to the client browser, but writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process passes the output data from the PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places in turn echo/pring-> PHP buffer-> TCP buffer-> browser
PHP output_buffering---ob_flush ()
By default, PHP buffer is turned on, and the buffer default is 4096, or 1kb. You can find the output_buffering configuration in the php.ini configuration file. When Echo,print output user data, the output data will be written to the PHP output_buffering until Output_ The buffering is full, sending the data over TCP to the browser display. You can also manually activate the PHP output_buffering mechanism by Ob_start (), so that even if the output exceeds 1KB data, it does not really send the data to TCP to the browser, because Ob_start () the PHP buffer space set to large enough. The data is sent to the client browser only until the end of the script, or when the Ob_end_flush function is invoked.
The use of these two functions, even if many people are most puzzled by the problem, the manual on the two functions of the explanation is vague, did not explicitly point out their differences, it seems that both of the function is to refresh the output cache. However, if Fush () is replaced with Ob_flush () in the code at the beginning of our article, the program can no longer execute correctly. Obviously, they are different, otherwise the manual also directly shows that one of them is another function of the alias can be, there is no need to explain separately. So what is the difference between them?
When the cache is not turned on, the contents of the script output are in the state waiting for output on the server side, and flush () can send the content waiting for output to the client immediately.
When the cache is turned on, the contents of the script output are stored in the output cache, and there is no content in the waiting output state, and you do not send anything to the client directly using flush (). and the role of Ob_flush () is the original output cache content out, set to wait for the output state, but not directly to the client, then you need to use Ob_flush () and then use Flush (), the client can immediately obtain the output of the script.
The correct sequence of flush and ob_flush, correct should be, first Ob_flush flush again, as follows:
Ob_flush ();
Flush ();
If the Web server's operating system is a Windows system, the order is reversed or the Ob_flush () does not appear to be problematic. [To be verified] but you cannot flush the output buffer on Linux.
Output buffering function
1.bool Ob_start ([Callback $output _callback [, int $chunk _size [, BOOL $erase]]]
Activates the output_buffering mechanism. Once activated, the script output is no longer directed to the browser, but is temporarily written to the PHP buffer memory area.
PHP opens the output_buffering mechanism by default, except that by invoking the Ob_start () function, the output_buffering value is extended to large enough. You can also specify $chunk_size to specify the value of Output_buffering. The default value for $chunk _size is 0, which means that the data in PHP buffer will not be sent to the browser until the script is finished running. If you set the size of the $chunk_size, the data in buffer will be sent to the browser as long as the length of the data in the buffer reaches the value.
Of course, you can process the data in the buffer by specifying $ouput_callback. For example, function Ob_gzhandler, compressed data in the buffer and then sent to the browser.
The third parameter: whether to erase the cache, optionally, the default is true, and if set to False, the cache will not be purged until the execution of the script completes.
2.ob_get_contents
Gets a copy of the data in the PHP buffer. It is worth noting that you should call the function before the Ob_end_clean () function is called, otherwise ob_get_contents () returns an empty character.
You can use Ob_get_contents () to get the server-side cached data as a string.
Using Ob_end_flush () outputs the cached data and closes the cache.
Using Ob_end_clean () silently clears the server-side cached data without any data or other behavior.
The server-side cache is stacked, which means that you can open another cache Ob_start () in the Ob_start () before closing it.
But you also have to be sure to turn off the cached operation as much as the number of operations that turn on the cache.
Ob_start () can specify a callback function to handle cached data, and if a ob_start () is nested within another ob_start (), we assume that the outer ob_start (), the number is a, and the inner Ob_start () number is B, Each of them developed a callback function is Functiona and functionb, then in the cache B data output, it will ancestors funcitonb callback function processing, and then to the outer Functiona callback function processing, before the output to the client.
In addition, the manual said that for some Web servers, such as Apache, the use of callback functions may change the current working directory of the program, the solution is to manually modify the work directory back in the callback function, with the ChDir function, this seems not often encountered, encountered when you remember to check the manual it.
3.ob_end_flush and Ob_end_clean
These two functions are somewhat similar and will close the ouptu_buffering mechanism. But the difference is that Ob_end_flush simply flushes the data in the PHP buffer (flush/send) to the client browser, while Ob_clean_clean empties the data in the PHP bufeer (erase), but does not send it to the client browser.
Before the Ob_end_flush call, the data in the PHP buffer still exists, and ob_get_contents () can still get a copy of the data in the PHP buffer.
After the Ob_end_flush () call Ob_get_contents () takes an empty string, and the browser does not receive the output, that is, there is no output.
The
can use Ob_get_contents () as a string to get the data for the server-side cache, and using Ob_end_flush () outputs the cached data and closes the cache. The
uses Ob_end_clean () to silently purge the server-side cached data without any data or other behavior.
Server-Side caching is stacked, which means that you can open another cache Ob_start () in the Ob_start () before shutting down. But you also have to be sure to turn off the cached operation as much as the number of operations that turn on the cache.
Ob_start () can specify a callback function to handle cached data, and if a ob_start () is nested within another ob_start (), we assume that the outer ob_start (), the number is a, and the inner Ob_start () number is B, Each of them developed a callback function is Functiona and functionb, then in the cache B data output, it will ancestors funcitonb callback function processing, and then to the outer Functiona callback function processing, before the output to the client.