duanlu2935 2017-09-28 08:54
浏览 94

为什么不ob_flush和flush工作立即输出到浏览器?

I try to output data from PHP to the browser immediately everytime there is some data to send. I've searched for a solution and ended up with the code below where each line should be shown in the browser every second.

This should work according to the PHP manual and other sites. But for me it doesn't. The browser waits for 5 seconds and displays everything at once. I have tried on latest versions of Firefox, Chrome and IE and I can't figure out why the result isn't shown after each ob_flush/flush. Can anyone explain?

<?php
header( 'Content-type: text/html; charset=utf-8' );
ob_implicit_flush(true);
ob_start();

for ($i = 0; $i<5; $i++){
    echo "<p>{$i}. Displaying one line...</p>";
    echo str_pad('',4096)."
";   

    ob_flush();
    flush();

    sleep(1);
}

echo "Done.";
ob_end_flush();
?>

Note: I have also experimented on using ob_end_flush at the beginning, removing the header line, sending longer echo strings, read other posts on Stack Overflow, etc.. The browser still waits until the output is done.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起