English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP scandir() Function Usage and Example

PHP Directory Reference Manual

The scandir() function lists files and directories in the specified path

Syntax

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

Definition and Usage

It returns an array of files and directories from the passed directory.

Parameter

NumberParameters and Description
1

directory (required)

is the directory to be traversed.

2

sorting_order (optional)

It specifies the sorting order. The default value is 0 (ascending). If set to1means descending order.

3

context (optional)

The environment handle of the specified directory. The context is an option that can modify the behavior of the directory stream.

Return value

 Returns an array containing file names on success, FALSE on failure. If the directory is not a directory, it returns a boolean FALSE and generates an E_WARNING level error.

Online Example

Here is the usage of this function, listing files and directories in the newfolder directory:

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

Output result:

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