PHP Directory scandir() Function



The PHP Directory scandir() function is used to get all of the files in the current or given directory. This function returns an array of files and directories from the given directory.

The directory, stream behavior, and sorting_order of the files and directories are given as parameters to the scandir() function, which returns an array of filenames on success and false on failure.

The sorted order is alphabetical in ascending order by default. The sort order is alphabetical in descending order if the optional sorting_order is set to SCANDIR_SORT_DESCENDING. The result is unsorted if SCANDIR_SORT_NONE is used.

Syntax

Here is the syntax of the PHP Directory scandir() function −

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

Parameters

The parameters are needed to use the scandir() function are mentioned below −

Sr.No Parameter & Description
1

directory(Required)

The directory that will be scanned.

2

sorting_order(Optional)

It specifies the sort order. Default is 0 (ascending). If set to 1, it indicates descending order.

3

context(Optional)

It specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream.

Return Value

It returns an array of filenames on success, or FALSE on failure.

PHP Version

The scandir() function was introduced in core PHP 4 and is compatible with PHP 5, PHP 7, and PHP 8.

Example

We will use the PHP Directory scandir() function to list all the files and directories present in the specified and given directory path.

The program will print the content in ascending order and then in descending order.

<?php
   $dir    = '/newfolder';
   $files1 = scandir($dir);
   $files2 = scandir($dir, 1);
   
   print_r($files1);
   print_r($files2);
?> 

Output

This will produce the following result −

Array (
   [0] => .
   [1] => ..
   [2] => abc.php
   [3] => bbc.txt
   [4] => somedir
)
Array (
   [0] => somedir
   [1] => indiabbc.txt
   [2] => status999.php
   [3] => ..
   [4] => .
)

Example

In the below PHP code we will list the content in descending order only using the scandir() function and using its parameter called SCANDIR_SORT_DESCENDING.

<?php
   $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac";
   $contents = scandir($directory, SCANDIR_SORT_DESCENDING);

   foreach ($contents as $item) {
      echo $item . "<br>";
   }
?> 

Output

This will produce the following result −

new dir
myfile.txt
my.php
logo.gif
index.php
images
image.gif
.DS_Store
..
.

Example

Here we will use scandir() function to scan the directory and with the help of is_dir() function we will verify the directories available in the given path and print their names.

<?php
   // enter your working directory path here
   $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac";

   // now scan the directory
   $items = scandir($directory);

   // print the directory here using foreach loop
   echo "Directories are: " . "<br>";
   foreach ($items as $item) {
      $dir = "$directory/$item";
      if (is_dir($dir)) {
         echo $item . "<br>";
      }
   }
?> 

Output

This will lead to the following outcome −

Directories are:
.
..
images
new dir

Example

In the below PHP code we will use scandir() function to scan the directory and with the help of is_file() function we will verify the files available in the given path and show their names.

<?php
   // enter your working directory path here
   $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac";
   $items = scandir($directory);
   echo "Files are: " . "<br>";
   foreach ($items as $item) {
      $filePath = $directory . '/' . $item;
      if (is_file($filePath)) {
         echo $item . "<br>";
      }
   }
?> 

Output

The result of this PHP code is −

Files are:
.DS_Store
image.gif
index.php
logo.gif
my.php
myfile.txt

Note

  1. scandir() returns the current directory (.) and the parent directory (..).
  2. An incorrect directory path given to scandir() generates an E_WARNING error and returns FALSE.
  3. scandir() is a shorter alternative to the time-consuming readdir() method, which reads one directory entry at a time.
  4. The is_file() function can be used to limit directory entries to just files.
  5. Use the is_dir() function to separate directories from directory entries.
  6. Use the array_diff() function to delete entries from the result array.

Summary

The scandir() function in PHP is useful for listing files and directories within a specified directory. It is easy to use and provides for variable sorting of results.

php_function_reference.htm
Advertisements