【问题标题】:PHP progress bar shows only after script completionPHP 进度条仅在脚本完成后显示
【发布时间】:2013-04-23 08:58:19
【问题描述】:

我正在尝试从http://w3shaman.com/article/php-progress-bar-script 运行示例。

我在 Windows 上使用quickPHP。以上教程有直播Demolink,运行良好。

问题:

当我测试这个脚本时,只有在 PHP 完成处理时我才会得到 bar。我也尝试使用 jQuery UI 进度条,但 PHP 在显示进度条之前变得很忙,然后突然显示 100 完成。怎么了?

来自above link的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;

// Loop through process
for($i=1; $i<=$total; $i++){
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </script>';

    // This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);

    // Send output to browser immediately
    flush();

    // Sleep one second so we can see the delay
    sleep(1);
}

// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</body>
</html>

【问题讨论】:

    标签: php progress-bar


    【解决方案1】:

    我认为现在,QUICK PHP 不会让您获得此处所述的中间结果:http://www.zachsaw.com/forum/viewtopic.php?f=11&t=27&hilit=flush(我知道这是一篇旧帖子,但我找不到任何最近在 QuickPHP 上使用刷新的参考)。

    似乎由于 QuickPHP 处理 PHP 输出的方式,它只在完整的脚本执行完成后才发送结果。这就是为什么您要完成该栏的原因,因为您的脚本依赖于在 php 脚本仍在运行时继续向 Web 浏览器发送数据的可能性。

    所以答案是,现在不可能这样做。

    【讨论】:

      【解决方案2】:

      尝试使用这个:

      //At the beginning of your PHP file
      ob_start();
      ...
      

      然后:

      ob_flush();
      flush();
      

      而不是

      flush();
      

      【讨论】:

      • 这很奇怪:它对我来说很好用。虽然我有一个想法:尽量不要使用echo,像这样:... ?&gt; &lt;script&gt;your_js_code_here&lt;/script&gt; &lt;? ...
      • 不,我为我的项目使用成熟的 Apache 服务器
      【解决方案3】:

      我在工作中也遇到过类似的情况。我使用 jquery、json 来检查脚本执行的进度。 php 脚本将其状态写入一个临时文件。我 blogged 关于它,这里可能有一些错误,但做得很好。

      【讨论】:

        猜你喜欢
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        • 2016-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多