dstt1818 2013-06-20 08:55
浏览 12

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

i have a snippet of code like this :

 $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;

i want the messages to be displayed after every line of compress function call but right now, its running the each call of the functions in a bunch and then displaying the messages in a bunch at the end that too every line repeated.

How can i solve this?

  • 写回答

1条回答 默认 最新

  • douzhenchun6782 2013-06-20 09:03
    关注

    I think the others have missed the point of the question. You want each line of each to appear in "real time" as the script is processing rather than on execution completion. PHP by default doesn't output the buffer until the script execution has been completed or the buffer is full.

    You can manually flush the buffer to screen when you want using this function.

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

    And you use it in your code like this

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

    Also just echo out the lines directly ratehr than assigning them to $response then echoing out that.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么