PHP Function Readdir
The php function readdir allows you to iterate over a directory and get all of the files within. This can be useful for many reasons including displaying all of the files in a certain directory on your website. For example, you may want to create a form that allows users to see all of the files in your Toys or Vacations directory. This would work like an FTP on your own server so that users can click on the file they are interested in downloading.
In order to use the php readdir function you need to have the path to the directory you want to work with open. Then the php function readdir() will take this directory handle resource and return the entry name on success or FALSE.
This is a basic usage of readdir() but it can be used in more advanced ways as well. For example, you can filter the list of files by their extensions or sort them alphabetically or based on file modification date.
You can also take advantage of the SPL functions that provide an iterator for traversing directories by using an array filter with is_file(). This way you can check each file or directory entry for being a file and only process them if they are.
Another useful function in this class is the rewinddir() function which resets the directory handle that was opened with the opendir() function. This can be helpful if you are working with a large number of files and don’t want to waste time searching for duplicated files.