Jump to content

Counting files in a directory


NotionCommotion
Go to solution Solved by Psycho,

Recommended Posts

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());
Link to comment
Share on other sites

  • Solution

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.

  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.