TLDR, i'm writing some code in PHP to count the total of my files and folder in the directory where my project is located at.
I have around 15.000 files and 1000 folder, but these queries took me arounds 6 seconds and 1 second for the pages, so in total i have to wait 6-8 seconds for the page to load and finish up.
Is there anyway to speed this up ? I have tried using array_column as per in this thread: PHP Foreach loop too slow when applied on large arrays
But still showing the same, still took 6 seconds.
Here's some of my code.
$files= 0;
$dir       = glob("../");
foreach ($dir as $obj) {                    
    if (is_dir($obj)) {
        $folders++;
        $scan = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($obj, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($scan as $file) {
            if (is_file($file)) {
                $files++;
                $exp = explode(".", $file);
                $exp2 = isset($exp[3]) ? $exp[3] : null;
                if ($exp2 == "txt") {
                    $files+;
                }
            } else {
                $folders++;
            }
        }
    } else {
        $files++;
    }
}
unset($exp2);
Where my $exp variable are consisting of 4 index.
array ( 
0 => '', 
1 => '', 
2 => '\\dashboard\\image37', 
3 => 'txt', )
 
    