PHP scandir() Function

What is PHP scandir() Function?

If you want to get all the entries (ex. files, folders) of a directory as an array, use scandir() function. Just mention the directory path and the function does its job. “scandir” is short for “Scan the Directory”.

Syntax:

scandir(directory, sorting_order, context)

Parameters:

The Function has 1 required parameter and 2 optional parameters-

directory (Required): A directory path whose entries the function reads. Instead of mentioning current directory path, you can use dot(.). Similarly, double dot(..) mention the parent directory of the current one. Check example 2.

sorting_order (Optional): The order in which the function sorts the directory items in the array. It has three values-

  • SCANDIR_SORT_ASCENDING (or 0): It sorts the directory entries alphabetically in ascending order. it the default order i.e. if you don’t mention this parameter, the function sorts entries in this order. Check example 1.
  • SCANDIR_SORT_DESCENDING (or 1): It sorts the directory entries alphabetically in descending order. Check example 1.
  • SCANDIR_SORT_NONE: It doesn’t sort the directory items. Check example 1.

context (Optional): A context stream resource. It allows you to define context of the directory handle. Context is a set of options that can modify the behavior of a stream.

Return Values:

The function returns-

  • An array of directory entries (ex. files, folders) – on success
  • FALSE – on failure.

Examples:

Example 1: Retrieving directory entries in different orders-

<pre>
<?php
$dirPath = "file";
$entries = scandir($dirPath);
print_r($entries);
$entries = scandir($dirPath, SCANDIR_SORT_ASCENDING);
print_r($entries);
$entries = scandir($dirPath, SCANDIR_SORT_DESCENDING);
print_r($entries);
$entries = scandir($dirPath, 1);
print_r($entries);
$entries = scandir($dirPath, SCANDIR_SORT_NONE);
print_r($entries);
?>
</pre>

Output:

Array
(
    [0] => .
    [1] => ..
    [2] => abc.jpg
    [3] => function.docx
    [4] => office
    [5] => tel.jpg
    [6] => xyz.jpg
)
Array
(
    [0] => .
    [1] => ..
    [2] => abc.jpg
    [3] => function.docx
    [4] => office
    [5] => tel.jpg
    [6] => xyz.jpg
)
Array
(
    [0] => xyz.jpg
    [1] => tel.jpg
    [2] => office
    [3] => function.docx
    [4] => abc.jpg
    [5] => ..
    [6] => .
)
Array
(
    [0] => xyz.jpg
    [1] => tel.jpg
    [2] => office
    [3] => function.docx
    [4] => abc.jpg
    [5] => ..
    [6] => .
)
Array
(
    [0] => .
    [1] => ..
    [2] => abc.jpg
    [3] => function.docx
    [4] => office
    [5] => tel.jpg
    [6] => xyz.jpg
)

Explanation:

Line 4: Without the second parameter, the function sort the entries in ascending order.

Line 6: As the second parameter is SCANDIR_SORT_ASCENDING sorts, the function sorts the entries in ascending order.

Line 8: As the second parameter is SCANDIR_SORT_DESCENDING sorts, the function sorts the entries in descending order.

Line 10: As the second parameter is 1 (same as SCANDIR_SORT_DESCENDING) sorts, the function sorts the entries in descending order. Line 12: As the second parameter is SCANDIR_SORT_NONE, the function doesn’t sort the entries.

Example 2: Reading all entries from the current directory-

<pre>
<?php
$dirPath = ".";
$entries = scandir($dirPath);
print_r($entries);
?>
</pre>

Output:

Array(
    [0] => .
    [1] => ..
    [2] => about.php
    [3] => contact.php
    [4] => disclaimer.php
    [5] => privacy.php
    [6] => service.php)

Explanation:

“.” indicates current directory. So, the scandir() function returns the entries of the current directory.

Example 3: Reading directory entries with wrong file path-

<?php
$dirPath = "invalidDirectory";
$entries = scandir($dirPath);
print_r($entries);
?>

Output:

Warning: scandir(invalidDirectory): The system cannot find the file specifi (code: 2) in D:\xampp\htdocs\php\scandir.php on line 3
Warning: scandir(invalidDirectory): Failed to open directory: No such file or directory in D:\xampp\htdocs\php\scandir.php on line 3
Warning: scandir(): (errno 2): No such file or directory in D:\xampp\htdocs\php\scandir.php on line 3

Explanation:

As the directory invalidDirectory doesn’t exist, the scandir() function returns warning.

Example 4: Retrieving only files from a directory-

<pre>
<?php
$dirPath = "file";
$entries = scandir($dirPath);
echo "Files are: " . "<br />";
foreach ($entries as $entry) {
    $filePath = $dirPath . '/' . $entry;
    if (is_file($filePath)) {
        echo $entry . "<br>";
    }
}
?>
</pre>

Output:

Files are:
abc.jpg
function.docx
shorishartel.jpg
xyz.jpg

Explanation:

Is_file() function returns only the files from all the entries.

Example 5: Retrieving only folders from a directory-

<pre>
<?php
$dirPath = "file";
$entries = scandir($dirPath);
echo "Directories are: " . "<br />";
foreach ($entries as $entry) {
    $dir = "$dirPath/$entry";
    if (is_dir($dir)) {
        echo $entry . "<br>";
    }
}
?>
</pre>

Output:

Directories are:
.
..
office

Explanation:

is_dir() function returns only the directories from all the entries.

Example 6: Retrieving entries without do entries (. and ..)-

<pre>
<?php
$dirPath = "file";
$entries = array_diff(scandir($dirPath), array('.', '..'));
print_r($entries);
?>
</pre>

Output:

Array(
    [2] => abc.jpg
    [3] => function.docx
    [4] => office
    [5] => tel.jpg
    [6] => xyz.jpg)

Explanation:

The array_diff() function returns all the entries from the first array except the ones that are in the second array.

Notes on scandir() Function:

  1. In the output of the examples, you see single dot (.) and double dot(..) as output. Here, single dot means the current directory that you’re using and double dot means parent directory of the current directory which is immediately above the working directory.
  2. If the supplied directory path is not a valid directory path, the function generates an E_WARNING error alone with returning FALSE. Check example 3.
  3. scanir() is a short alternative of the repetitive readdir() function. The readdir() function read one directory entry at a time, so you need to run a loop to read the entries.
  4. From all types the directory entries, to get only the files you can use is_file()  function. Check example 4.
  5. From all types the directory entries, to get only the folders you can use is_dir()  function. Check example 5.
  6. To remove the single dot(.) and double dots(..) from the entries, you can use array_diff() function. Check example 6.

Caution:

If you run this function recursively in a directory that contains of large number of files and folders, it may consume extra RAM and slow down your application.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP scandir() Function

scandir() is a very common and useful function to know all the content of a directory quickly and sorting the items in any order. it is a useful built-in directory function.

Reference:

https://www.php.net/manual/en/function.scandir.php