array scandir ( string dirname [, int sorting_order [, resource context]])
Scandir() is a neat function that takes a minimum of one parameter with an optional second. Parameter one is the path of a directory you want to work with - scandir() returns an array of all files and directories in the directory you specify here. Parameter two, if included and set to 1, will sort the array returned reverse alphabetically - if it is not set, the array is returned sorted alphabetically.
This next script prints out a list of all the files and directories in the current directory, with reverse sorting:
<?php
$files = scandir(".", 1);
var_dump($files);
?>
Using scandir() is a quick alternative to calling readdir() repeatedly, and is particularly helpful when you use the second parameter.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Remote files >>
Previous chapter: Deleting directories
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.