PHP scandir() Function with Examples

The scandir() function in PHP is a built-in function that is used to read all files and directories inside a specified folder or directory. This function returns an array containing the name of all files and directories in the specified files.

The scandir() function is commonly used in:

  • File management systems
  • Reading uploaded files from a folder
  • Searching specific file types
  • Creating directory-based galleries

Syntax of scandir() Function in PHP


The general syntax of the scandir() function in PHP is given below:

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

Parameters:

The scandir() function accepts three parameters:

  • $directory – This defines the path of the directory you want to scan.
  • $sorting_order – This is an optional parameter that defines the sorting order of results.
    • SCANDIR_SORT_ASCENDING (default) → A to Z
    • SCANDIR_SORT_DESCENDING → Z to A
    • SCANDIR_SORT_NONE → No sorting
  • $context – This is also an optional parameter used to modify the behavior of the stream. Usually not required.

Return Value:

  • The scandir() function returns an array containing files and directories on success.
  • Otherwise, this function returns false on failure.

Basic Examples of PHP scandir() Function


Let’s take some important examples based on the scandir() function provided by PHP.

Example 1: Basic

<?php
$files = scandir("uploads");
print_r($files);
?>
Output:
     Array
    (
      [0] => .
      [1] => ..
      [2] => file1.txt
      [3] => file2.jpg
      [4] => notes.pdf
    )

In this example, the first dot (.) refers to the current directory. The second double dot (..) refers to the parent directory. Other elements, such as file1.txt, file2.jpg, notes.pdf are the actual files in the uploads folder.

Example 2: scandir() Function with Descending Order

<?php
$files = scandir("uploads", SCANDIR_SORT_DESCENDING);
print_r($files);
?>
Output:
      Array
     (
       [0] => notes.pdf
       [1] => file2.jpg
       [2] => file1.txt
       [3] => ..
       [4] => .
     )

In this example, all the files are listed in reverse order (Z → A).

Example 3: Filtering Files and Directories

Most of the time, you don’t want to display . and … You can filter them out using built-in array_diff() function in PHP.

<?php
$files = scandir("uploads");
$files = array_diff($files, array('.', '..'));
print_r($files);
?>
Output:
      Array
      (
        [2] => file1.txt
        [3] => file2.jpg
        [4] => notes.pdf
      )

Now, only actual files remain.

Example 4: Display Files in a Loop

<?php
$files = array_diff(scandir("uploads"), array('.', '..'));
foreach ($files as $file) {
    echo $file . "\n";
}
?>
Output:
      file1.txt
      file2.jpg
      notes.pdf

Example 5: Filtering Specific File Types

Suppose you only want to display .jpg images from a folder.

<?php
$files = array_diff(scandir("uploads"), array('.', '..'));
foreach ($files as $file) {
   if (pathinfo($file, PATHINFO_EXTENSION) == "jpg") {
      echo $file . "<br>";
   }
}
?>
Output:
      file2.jpg

Example 6: scandir() with Subdirectories

You can also check if a scanned item is a file or directory using built-in is_dir() function. provided by PHP.

<?php
$items = array_diff(scandir("uploads"), array('.', '..'));
foreach ($items as $item) {
   if (is_dir("uploads/" . $item)) {
        echo "[Directory] " . $item . "<br>";
   } else {
        echo "[File] " . $item . "<br>";
   }
}
?>
Output:
      [File] file1.txt
      [File] file2.jpg
      [Directory] documents

Real-Time Use Cases of scandir() Function


Following are the real-time use cases of scandir() function in PHP applications.

  • File Manager – Display all files inside a directory for management.
  • Image Gallery – Scan an image folder and display all image files, such as .jpg, .png files, etc.
  • Search Functionality – Scan directories to find specific files.
  • Backup Tools – List files for compression or migration.
  • Security Checks – Detect unauthorized files uploaded to server directories.