PHP 刷新控制器中的信息

PHP flush information in controller

我正在尝试发送数据以从 PHP CodeIgniter 中的控制器查看。
我正在通过 ajax 调用 PHP 函数并使用 ob_flush 发送回数据,但问题是所有刷新调用都在以后的调用中连接在一起。

例如:第一次刷新发送1第二次刷新调用发送12而不是2

这是我的控制器循环。

foreach ($csv_array as $row) {
    ob_start();
    $varint=$varint+1;
    echo $varint;
    $content = ob_get_contents();
    ob_end_clean();
    echo $content;
    ob_flush();
    flush();
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
}

我的 ajax 电话是这样的:

document.getElementById('upld').onclick = function() {
    xhr = new XMLHttpRequest();
    var mydata = new FormData($('#form')[0]);
    xhr.open("POST", base_url+"index.php/controller/function", true);
    xhr.onprogress = function(e) {
        console.log(e.currentTarget.responseText);
    }
    xhr.onreadystatechange = function(data='') {
        if (xhr.readyState == 4) {
            console.log(myArr);
        }
    }
    xhr.send(mydata);
};

flush() 的文档说:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

在您的情况下,您可以将 flush() 替换为 ob_flush(),因为我认为您不希望刷新系统输出缓冲区。

作为额外的代码:

$content = ob_get_contents();
ob_end_clean();

可以压缩为一个函数调用,它是前面两个命令组合的别名:

$content = ob_get_clean();

Documentation for ob_get_clean()