【发布时间】: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