Why do I need `str_pad('',4096)` to make PHP flushing work?

随声附和 提交于 2019-12-13 17:02:46

问题


For example,

this doesn't work (Firefox 21, IE8):

<?php
function flush_buffers(){
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}  
ob_start();
echo 'Text 1<br />';
flush_buffers();
Sleep(2);
echo 'Text 2<br />';
flush_buffers();
Sleep(2);
echo 'Text 3<br />';
flush_buffers();
Sleep(2);
echo 'Text 4<br />';
?>

But this one works:

<?php
function flush_buffers(){
    echo str_pad('',4096);
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}  
ob_start();
echo 'Text 1<br />';
flush_buffers();
Sleep(2);
echo 'Text 2<br />';
flush_buffers();
Sleep(2);
echo 'Text 3<br />';
flush_buffers();
Sleep(2);
echo 'Text 4<br />';
?>

I have PHP 5.4.11 VC9 and Apache 2.4.3 (apacheLounge) running on Win XP SP3.


回答1:


Some browsers include their own internal buffer in order to download and display more efficiently with less choppiness. In most cases, this buffer is 4Kb, or 4096 bytes.

What str_pad('',4096) does is write 4,096 spaces to the output. Since it's HTML, these spaces collapse into a single space.

Overall, this behaviour should NOT be relied upon. Browsers are for viewing webpages, not bastardising into console terminals.

Also, why are you writing </br>? There is no such thing as an end <br> tag, and the self-closing version is <br />




回答2:


In addition to specifying your browser version, you should've also specified the web server PHP is running on. That's where the output buffering is probably happening, after all.

If you read the documentation on flush, you'll see that it's not a guaranteed operation. Kind of like a disk write where even if you have the library and the OS cooperating, there's still the disk hardware to (possibly) buffer it. Only the most meticulous (and thus nigh-impossible to port) methods will work, and not even that is guaranteed.

Edit: Oh yes, the browser (or hell, some router or TCP stack in between) can do it, too.




回答3:


It is because the render engine of the browser need to get enough data when it decide to render the content to the screen. And how much data is needed is the decided by the browser.




回答4:


For a slow hosting, try something like this:

$totalUsers = count_users();

echo 'There are ', $result['total_users'], ' total users<br />';
foreach($result['avail_roles'] as $role => $count)
    echo ', ', $count, ' are ', $role, 's';
echo '.';

$batchSize = 25;
$batchesCount = ceil($totalUsers['total_users'] / $batchSize);
$Users = array();
for ($count = 0; $count < $batchesCount; $count++) {
$args = array('number' => $batchSize, 'offset' => $count * $batchSize);
    $batchNewUsers = get_users($args);
    $Users = array_merge($Users, $batchNewUsers);
    sleep(1); # reduce load
    echo '.'.str_pad(' ',4096);# keep alive
}

echo '<br />All done.';

Else, you might be better a querying the database directly, or getting the ID's first and query each by ID: https://wordpress.stackexchange.com/questions/231003/how-to-get-list-of-all-users-there-metadata



来源:https://stackoverflow.com/questions/14574432/why-do-i-need-str-pad-4096-to-make-php-flushing-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!