Skip to content

Parsing a Directory

Parsing directory: scandir()

If you need to obtain the list of files in a directory (like ls command does) you can use the scandir() function.

$files = scandir(__DIR__);
var_dump($files);

A directory name (string) as input will get you an array of strings: filenames, directory names, also including special directories like ”.” and ”..β€œ.

You can control the sorting with 2 constants. The default is an ascending sorting (alphabetical). With SCANDIR_SORT_DESCENDING you can obtain string with a descending order:

$files = scandir(__DIR__, SCANDIR_SORT_DESCENDING);
var_dump($files);

Or, if you don’t want to sort at all, you can use SCANDIR_SORT_NONE:

$files = scandir(__DIR__, SCANDIR_SORT_NONE);
var_dump($files);

If you don’t want the ”.” and ”..” in your list, eliminating the first 2 items is wrong, because most of the time in alphabetical order ”.” and ”..” are in the 2 first positions. But, what if you have a filename that begins with ”-” for example?

So my suggestion is to drop 2 elements by value. I have in mind 2 ways, the first one is looping through elements and skipping ”.” and ”..β€œ. The most elegant way is using the array_diff() function:

$files = array_diff(scandir(__DIR__), [".", ".."];
var_dump($files);