NotionCommotion Posted December 29, 2017 Share Posted December 29, 2017 Looks like both http://php.net/manual/en/class.filesystemiterator.php and http://php.net/manual/en/globiterator.count.php (as well as other solutions) can be used. Is one approach more suitable than the other? $fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS); printf("There were %d Files", iterator_count($fi)); $iterator = new GlobIterator('*.xml'); printf("Matched %d item(s)\r\n", $iterator->count()); Quote Link to comment https://forums.phpfreaks.com/topic/306021-counting-files-in-a-directory/ Share on other sites More sharing options...
Solution Psycho Posted December 30, 2017 Solution Share Posted December 30, 2017 They are basically the same thing: GlobIterator extends FilesystemIterator FileSystemIterator, on it's own, will return all items in a directory (with the exception of being able to exclude the '.' and '..'). GlobIterator extends the function so you can have it only return specific items matching a pattern. Look at your own example. The GlobIterator will only count files (or directors) ending with '.xml'. So, if you simple want to count all elements in a directory use FileSystemIterator. Else, if you are only looking for specific folders/files matching a pattern use GlobIterator. 2 Quote Link to comment https://forums.phpfreaks.com/topic/306021-counting-files-in-a-directory/#findComment-1554977 Share on other sites More sharing options...
requinix Posted December 30, 2017 Share Posted December 30, 2017 Remember there are non-OOP methods too. count(scandir(__DIR__) - 2) count(glob("patt*.ern")) Quote Link to comment https://forums.phpfreaks.com/topic/306021-counting-files-in-a-directory/#findComment-1554981 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.