0

我有一段这样的代码:

 $response = "<div style='font-size:18px;margin-top:2em;text-align:center;color:#173f5f;>";
 $response .= "Intializing sequence...";
    echo $response;
    $response .= "Starting compression of folders...";
    echo $response;
    $response .= "Compressing all photos now...";
    echo $response;
    $ph = compress('photos');
    $response .= "Photo compression complete.";
    $response .= "Compressing all signatures now...";
    echo $response;
    $sg = compress('signatures');
    $response .= "Signature compression complete.";
    $response .= "Compressing all excel files now...";
    echo $response;
    $excel = compress('uploads');
    $response .= "Excel files compression complete.</div>";
    echo $response;

我希望在每一行compress函数调用之后显示消息,但现在,它运行一堆函数的每个调用,然后在最后显示一堆消息,每一行都重复。

我该如何解决这个问题?

4

1 回答 1

0

我认为其他人错过了问题的重点。您希望每行的每一行在脚本正在处理而不是在执行完成时“实时”出现。PHP 默认不输出缓冲区,直到脚本执行完成或缓冲区已满。

您可以在需要使用此功能时手动将缓冲区刷新到屏幕。

function fcflush()
{
    static $output_handler = null;
    if ($output_handler === null) {
        $output_handler = @ini_get('output_handler');
    }
    if ($output_handler == 'ob_gzhandler') {
        // forcing a flush with this is very bad
        return;
    }
    flush();
    if (function_exists('ob_flush') AND function_exists('ob_get_length') AND ob_get_length() !== false) {
        @ob_flush();
    } else if (function_exists('ob_end_flush') AND function_exists('ob_start') AND function_exists('ob_get_length') AND ob_get_length() !== FALSE) {
        @ob_end_flush();
        @ob_start();
    }
}

你像这样在你的代码中使用它

echo "<div style='font-size:18px;margin-top:2em;text-align:center;color:#173f5f;>";
fcflush();
echo "Intializing sequence...";
fcflush();
echo "Starting compression of folders...";
fcflush();
echo $response .= "Compressing all photos now...";
fcflush();
    $ph = compress('photos');
echo "Photo compression complete.";
fcflush();
echo "Compressing all signatures now...";
fcflush();
    $sg = compress('signatures');
echo "Signature compression complete.";
fcflush();
echo "Compressing all excel files now...";
fcflush();
    $excel = compress('uploads');
echo "Excel files compression complete.</div>";
fcflush();

也只是直接回显这些行 ratehr 而不是将它们分配给 $response 然后回显。

于 2013-06-20T09:03:48.737 回答