即時輸出PHP緩衝區內容

即時輸出PHP緩衝區內容

<?php
header('X-Accel-Buffering: no');    //關閉buffer
header('Content-type: text/html;charset=utf-8');    //設置網頁編碼
ob_start(); //打開輸出緩衝控制
echo str_repeat(' ',1024*4);    //字符填充
/**
//給div定義的寬爲1000
$width = 1000;
//要顯示的一段html的字符串
$html = '<div style="padding: 0; background-color: white; border: 1px solid navy; width: %upx"><div id="progress" style="padding: 0; background-color: #FFCC66; border: 0; width: 0px; text-align: center; height: 16px"></div></div><div id="percent" style="position: relative; top: -16px;left:-175px; text-align: center; font-weight: bold; font-size: 8pt">0%%</div>';

echo sprintf($html, $width, $width);
echo ob_get_clean();    //獲取當前緩衝區內容並清除當前的輸出緩衝
flush();   //刷新緩衝區的內容,輸出
$length = 11;
for($i=0; $i<$length; $i++) {
    sleep(1);
    $proportion = ($i + 1) / $length;
    if ($i + 1 == $length) {
        $msg = '同步完成';
    } else {
        $msg = '正在處理';
    }
    $script = '<script>document.getElementById("percent").innerText="%u%%";document.getElementById("progress").style.width="%upx";</script>';
    echo sprintf($script, intval($proportion * 100), intval(($i + 1) / $length) * $width);
    echo ob_get_clean();    //獲取當前緩衝區內容並清除當前的輸出緩衝
    flush();   //刷新緩衝區的內容,輸出
}
*/
for($i=1;$i<=5;$i++){
        echo str_repeat($i,$i)."<br/>";
        echo ob_get_clean();
        flush();
        sleep(1);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章