PHP scandir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Directory

Definition

In PHP, scandir() returns an array of file and folder names from a path. It is the quickest way to list a folder.

scandir() does the work of opendir(), readdir() and closedir() in one step, and it sorts the names for you. The array includes the special entries '.' and '..', so beginners usually remove them. If the folder cannot be read, scandir() returns false.

Syntax

Call scandir() with the values you need. Start with this line.

PHP
$files = scandir($folder);
CallWhat it does
scandir($folder)Returns the names sorted in ascending (alphabetical) order.
scandir($folder, SCANDIR_SORT_DESCENDING)Returns the names sorted in descending (reverse) order.
array_diff(scandir($folder), ['.', '..'])Returns the list without the special entries '.' and '..'.

scandir() returns only names, not full paths. Join the folder path and the name yourself when you want to open or check a file. SCANDIR_SORT_NONE skips sorting, but the resulting order is not guaranteed.

Library declarations
PHP
scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, ?resource $context = null): array|false

Parameters

ParameterDescription
directoryRequired. The path of the folder to list.
sorting_orderOptional. The sort order. SCANDIR_SORT_ASCENDING (0) is the default and sorts alphabetically. SCANDIR_SORT_DESCENDING (1) sorts in reverse. SCANDIR_SORT_NONE (2) does not sort at all.
contextOptional. A stream context created with stream_context_create(). Most scripts leave it out, and the default is null.

Return Value

Return ValueDescription
array|falseReturns an array of file and folder names, including '.' and '..'. Returns false on failure, for example when the path is not a folder, and PHP also raises an E_WARNING.

PHP Examples

Example 1 — Listing a Folder with scandir()

We create a demo folder with three files and list it. First we print the raw result, then a cleaned version.

PHP
<?php
$folder = __DIR__ . '/demo-scandir';
mkdir($folder);
foreach (['b.txt', 'c.txt', 'a.txt'] as $name) {
    file_put_contents($folder . '/' . $name, 'x');
}

$all = scandir($folder);
echo "All: ", implode(', ', $all), PHP_EOL;

$files = array_values(array_diff($all, ['.', '..']));
echo "Files: ", implode(', ', $files), PHP_EOL;
echo "Count: ", count($files), PHP_EOL;

foreach ($files as $name) {
    unlink($folder . '/' . $name);
}
rmdir($folder);

How It Works

  1. The files are created in a mixed order (b, c, a) to show that scandir() sorts them.
  2. The raw result starts with '. ' and '..', followed by the names in alphabetical order.
  3. array_diff() removes the two special entries, and array_values() renumbers the keys.
  4. count() now gives the number of real files, which is 3.

Example 2 — When the Folder Does Not Exist

scandir() returns false for a path it cannot read. Check first, so you never loop over false.

PHP
<?php
$missing = __DIR__ . '/no-such-folder';

$result = @scandir($missing);
var_dump($result);

if ($result === false) {
    echo "Could not scan the folder.", PHP_EOL;
}

echo "Safe check with is_dir(): ", is_dir($missing) ? 'scan it' : 'skip it', PHP_EOL;

How It Works

  1. The folder 'no-such-folder' does not exist, so scandir() returns false.
  2. The @ symbol hides the warning in this demo. In real code you should avoid the warning by checking first.
  3. We test the result with === false and print a friendly message.
  4. Using is_dir() before scandir() is a simple way to avoid the problem altogether.

Example 3 — Descending Order

The second argument controls the sort order. Here we list the same folder in reverse, and then without the dot entries.

PHP
<?php
$folder = __DIR__ . '/demo-scandir-order';
mkdir($folder);
foreach (['a.txt', 'b.txt', 'c.txt'] as $name) {
    file_put_contents($folder . '/' . $name, 'x');
}

$desc = scandir($folder, SCANDIR_SORT_DESCENDING);
echo "Descending: ", implode(', ', $desc), PHP_EOL;

$clean = array_values(array_diff($desc, ['.', '..']));
echo "Clean: ", implode(', ', $clean), PHP_EOL;

foreach ($clean as $name) {
    unlink($folder . '/' . $name);
}
rmdir($folder);

How It Works

  1. SCANDIR_SORT_DESCENDING sorts the names in reverse alphabetical order.
  2. Because '. ' and '..' are also sorted, they now appear at the end of the list.
  3. array_diff() removes them, and array_values() keeps the keys neat.
  4. The cleaned list is ready to use, for example to show the newest or last-named files first.

Frequently Asked Questions

It returns an array with the names of all files and folders in a directory. By default the names are sorted alphabetically.
They are real entries in every folder: '.' is the folder itself and '..' is its parent. Remove them with array_diff($list, ['.', '..']) if you only want real items.
It returns false and raises an E_WARNING. Check with is_dir() first, or compare the result with === false.
Pass SCANDIR_SORT_DESCENDING as the second argument. Use SCANDIR_SORT_NONE if you do not need any sorting.
scandir() is simpler for most beginners because you get a sorted array in one call. opendir() and readdir() are better when you want to handle one entry at a time, for example in a folder with a huge number of files.

Back to Directory Functions

Browse all PHP directory function tutorials.

Directory functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful