8.13.2 One last directory function: scandir()This is NOT the latest copy of this book; click here for the latest version.
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 see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|