【问题标题】:how to display php progressbar when reading data using php使用php读取数据时如何显示php进度条
【发布时间】:2019-12-06 00:05:49
【问题描述】:

下面的代码显示了下面php长时间运行代码的百分比进度条,并且工作正常。

<?php

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    $total = 25;
    $i = 0;

    echo json_encode(array('progress' => 0, 'count' => $i, 'total' => $total));
    flush();
    ob_flush();

    while ($i < $total) {

        $i++;
echo json_encode(array('progress' => (($i/$total)*100), 'count' => $i, 'total' => $total));
        flush();
        ob_flush();

        sleep(1);



    }
    exit();
}
?>

现在我想在扫描目录时显示进度条百分比。

这是扫描目录的工作代码

function sk($path){
    if(file_exists($path) && is_dir($path)){
        $files = glob($path ."/*");

            foreach($files as $file){
                if(is_file("$file")){
                    // Display only filename
                    echo "$file"  . "<br>";

                } else if(is_dir("$file")){
                    sk("$file");
                }
            }

    } else {
        echo "folder does not exist.";
    }
}

sk("C:/xampp/htdocs/data");

我的问题在这里:

当我按照下面的代码将目录函数传递到 while 循环 时,进度条百分比在前端通过 ajax 调用停止计数 谁能帮帮我。

<?php

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    $total = 25;
    $i = 0;

    echo json_encode(array('progress' => 0, 'count' => $i, 'total' => $total));
    flush();
    ob_flush();

    while ($i < $total) {
        $i++;

// pass directory functions
function sk($path){
    if(file_exists($path) && is_dir($path)){
        $files = glob($path ."/*");

            foreach($files as $file){
                if(is_file("$file")){
                    // Display only filename
                    echo "$file"  . "<br>";

                } else if(is_dir("$file")){
                    sk("$file");
                }
            }

    } else {
        echo "folder does not exist.";
    }
}

sk("C:/xampp/htdocs/data");





echo json_encode(array('progress' => (($i/$total)*100), 'count' => $i, 'total' => $total));
        flush();
        ob_flush();

        sleep(1);



    }
    exit();
}
?>

【问题讨论】:

    标签: php


    【解决方案1】:

    这就是我设法让它工作的方法

    $.ajax({
      xhr: function()
      {
        var xhr = new window.XMLHttpRequest();
        //Upload progress
        xhr.upload.addEventListener("progress", function(evt){
          if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            //Do something with upload progress
            console.log(percentComplete);
          }
        }, false);
        //Download progress
        xhr.addEventListener("progress", function(evt){
          if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            //Do something with download progress
            console.log(percentComplete);
          }
        }, false);
        return xhr;
      },
      type: 'POST',
      url: "/",
      data: {},
      success: function(data){
        //Do something success-ish
      }
    });
    

    来源:stackoverflow link

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      相关资源
      最近更新 更多