PHP - Function scandir()


Advertisements


Syntax

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

Definition and Usage

Returns an array of files and directories from the passed directory.

Parameters

Sr.No Parameter & Description
1

directory

Required. The directory that will be scanned.

2

sorting_order

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

3

context

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

Return Value

Returns an array of filenames on success, or FALSE on failure.

Example

Following is the usage of this function −

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

This will produce the following result −

Array (
   [0] => .
   [1] => ..
   [2] => bar.php
   [3] => foo.txt
   [4] => somedir
)
Array (
   [0] => somedir
   [1] => foo.txt
   [2] => bar.php
   [3] => ..
   [4] => .
)

php_function_reference.htm

Advertisements