Monday 26 August 2013

PHP Function scandir()

Syntax

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

Definition and Usage

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

Paramters

ParameterDescription
directoryRequired. The directory that will be scanned.
sorting_orderOptional. Specifies the sort order. Default is 0 (ascending). If set to 1, it indicates descending order
contextOptional. 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 following result:
Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

No comments:

Post a Comment