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();

また、行を $response に割り当ててからエコーするよりも、直接行をエコーアウトするだけです。

于 2013-06-20T09:03:48.737 に答える