To count how many files in a directoy, I have to open a directory, write a while loop to read the directory and keep the count number.
Is there a direct way to count how many files in a directory?
Thanks!
Try this...
<?php /* Initialize counter */ $count = 0; /* Set directory name */ $dir = "/bin"; /* Open directory and parse file list */ if (is_dir($dir)) { if ($dh = opendir($dir)) { /* Iterate over file list */ /* Print filenames */ while (($filename = readdir($dh)) !== false) { if (($filename != ".") && ($filename != "..")) { $count ++; echo $dir . "/" . $filename . "\n"; } } /* Close directory */ closedir($dh); } } echo $count." files listed."; ?>
🙂