• PHP Video Tutorials

PHP - Function scandir()



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.

Parameters

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.

Example

Following is the usage of this function −

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

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] => .
)
php_function_reference.htm
Advertisements