I have to make a function that lists all subfolders into a folder. I have a no-file filter, but the function uses scandir() for listing. That makes the application very slow. Is there an alternative of scandir(), even a not native php function? Thanks in advance!
PHP: scandir() is too slow
15.5k Views Asked by Emil Avramov At
2
There are 2 best solutions below
3

Don't write your own. PHP has a Recursive Directory Iterator built specifically for this:
http://php.net/manual/en/class.recursivedirectoryiterator.php
As a rule of thumb (aka not 100% of the time), since it's implemented in straight C, anything you build in PHP is going to be slower.
You can use readdir which may be faster, something like this:
May require some tweeking but this is essentially what scandir does, and it should be faster, if not please write an update as i would like to see if i can make a faster solution.
Another issue is if your reading a very large directory your filling an array up within the internal memory and that may be where your memory is going.
You could try and create a function that reads in offsets so that you can return 50 files at a time!
reading chunks of files at a time would be just as simple to use, would be like so: