在代码执行的每个步骤之后显示消息

时间:2013-06-20 08:55:39

标签: php

我有一段这样的代码:

 $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函数调用的每一行之后显示消息,但是现在,它在一堆中运行函数的每次调用,然后在每一行中显示消息重复一行。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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();

同样只是直接回显线路而不是将它们分配给$ response然后回应出来。