cnqncom
V2EX  ›  PHP

为啥我的 NGINX 就输出不下面这个进度条效果呢?

  •  
  •   cnqncom · Oct 12, 2017 · 3643 views
    This topic created in 3144 days ago, the information mentioned may be changed or developed.
    <?php

    //防止执行超时
    set_time_limit(0);

    //清空并关闭输出缓冲区
    ob_end_clean();

    //某些浏览器要达到足够的长度才输出,所以填充空格
    echo str_repeat(" ", 1000);

    //输出进度条样式
    echo '<div style="width: 960px;margin: 0 auto;text-align:center;"><div style="border:1px solid #000;width:500px;margin: 0 auto;"><div id="progress_bar">loading...</div></div></div>';

    //需要循环操作的数据总数
    $data_total = 12;

    //进度条的总长度
    $width = '500';

    //百分比初始数值
    $progress = 0;

    //百分比数值的递增值
    $per_progress = number_format(100 / $data_total, 0);

    //进度条初始长度值
    $iwidth = 0;

    //进度条长度的递增值
    $per_width = $width / $data_total;

    //循环输出数据,这里可根据实际操作进行更换
    for($i = 1; $i <= $data_total; $i++)
    {
    $iwidth += $per_width;
    $progress += $per_progress;

    echo '<script type="text/javascript"><!--
    ','var progress_bar = document.getElementById("progress_bar");','progress_bar.style.background="#FFCC66";','progress_bar.style.width ="' . $iwidth . 'px";',"progress_bar.innerHTML = '{$progress}%';",'
    // --></script>';
    sleep(1); //根据实际情况是否需要
    flush();
    }

    //输出进度条最大值
    echo '<script type="text/javascript"><!--
    ',"progress_bar.innerHTML = '100%';",'
    // --></script>';
    flush();






    我的 PHP 版本 5.6,是直接一次性显示出来,而不是进度的形式逐步输出!
    3 replies    2017-10-12 16:59:30 +08:00
    ToughGuy
        1
    ToughGuy  
       Oct 12, 2017
    恩 你的 NGINX?
    xiamingchong
        2
    xiamingchong  
       Oct 12, 2017
    需要把 nginx 的 buffer 关了,
    打开 nginx.conf,
    设置 proxy_buffering off;
    cnqncom
        3
    cnqncom  
    OP
       Oct 12, 2017
    @xiamingchong 谢谢你,尽管这样还是不行。

    后来我找到了方法:

    在输出前先输出一个 nginx 特定的 header:

    header('X-Accel-Buffering: no');
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4778 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 09:46 · PVG 17:46 · LAX 02:46 · JFK 05:46
    ♥ Do have faith in what you're doing.