PHP Function Scanddir
php function scandir returns list of files and directories inside a specified path. It can take one optional parameter, sorting_order. This can be set to 0 (alphabetically ascending order) or to 1 to sort the result in alphabetical descending order. It can also be set to SCANDIR_SORT_NONE to return the result unsorted. The function can be called with a directory handle obtained using the opendir() function. After calling the function, you must use the closedir() function to ensure system resources associated with the directory handle are properly released.
Compared to other solutions such as glob and mkdir, this is a quick way of listing all the files in a particular directory. But, the problem is that the results aren’t sorted. This can be a major headache for some, especially if you want to display the list of files in a web application.
To avoid this issue, you can always separate DIRS from FILES by using some PHP magic. This is done by changing the is_dir(path) function to is_dir(path[1:]). This will make the function work as expected, sorted by filemodificationtime.
For more tips and tricks, check out the free eBook 'Hacking with PHP'. Get over 1200 pages of hands-on learning today!