Scan Directory

PHP Directory Function Basics - Part 5

Chrys Forcha
Introduction
This is part 5 of my series, PHP Directory Function Basics. It is possible to get the list of items in a directory, without using the opendir(), readdir() and closedir() functions. PHP has the scandir() function for this purpose. In this part of the series I show you how to use the scandir() function.

Note: If you cannot see the code or if you think anything is missing in this article (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Traditional Way of obtaining Directory List
The following code will copy the names of items from a directory into an array.

This is the traditional method of reading items from a directory. It is still useful. The first statement creates an array. In the while-loop, the names of the items are read into the array. The last statement sends the array content to the browser. What I want you to note here is that you have the opendir(), readdir() and closedir() functions. You also have the while-loop.

The scandir() Function
The scandir() function does what the above code does in a single statement. Well, the scandir() function does not have the print_r() feature; that is, it does everything that the above code does except printing to the browser.

In simple terms, the syntax of the scandir() function is,

array scandir ( string $directory [, int $sorting_order])

Here, the argument, $directory is the path of the directory in a string. Of course the path in the string ends with the name of the directory of interest. The function returns an array on success or false on failure. If it returns false, you can send an error message. The scandir() function has the advantage that it returns the array with the items sorted in alphabetical order. The scandir() function has the optional argument, $sorting_order. When $sorting_order is absent, the array returned is sorted alphabetically in ascending order. When it is present, it takes the value 1; in this case the array returned is still sorted alphabetically, but this time in descending order.

The following code does all what the above code does with fewer statements:

There are two advantages here over the previous code: a) This code is shorter; b) the array is sorted. As you can see the scandir() function solves the problem that opendir(), readdir() and closedir() together solve. Note that in the if-condition of this last code, the assignment and not the comparison operator is used, with the scandir() function. This looks ambiguous, but just accept it; that is what is in the PHP specification.

You can try the above two code samples. This is it for this part of the series. Let us stop here and continue in the next part of the series.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.