How to quickly find the number of files in a directory?

I have thousands of log files in several directories on my system. Basically I need to know only the number of these files in the directory. I am afraid this attitude:

$files = count(scandir("logs")) - 2;

Not very efficient (memory, file system). I am considering some global counter (added after adding a file) if there is no other efficient way?

+3
source share
4 answers
$files = count(glob("logs/*",GLOB_NOSORT));
+3
source

Try calling the system file list:

$files = exec('ls logs | wc -l');
+2
source

, opendir/readdir?

<?php
$i = 0;
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))
        if ($file != "." && $file != "..")
            ++$i;
    closedir($handle);
}
?>
+1

" ":

file_put_contents("counter_myDir","1",FILE_APPEND);

kB ( dirs, ), :

$files = filesize("counter_myDir");

? .

+1

All Articles