PHP scandir()Functions
PHP Directory Reference Manual
Examples
List the files and directories in the images directory:
<?php
$dir = "/images/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir,1);
print_r($a);
print_r($b);
?>
$dir = "/images/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir,1);
print_r($a);
print_r($b);
?>
Result:
Array
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
[4] => horse.gif
[5] => myimages
)
Array
(
[0] => myimages
[1] => horse.gif
[2] => dog.gif
[3] => cat.gif
[4] => ..
[5] => .
)
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
[4] => horse.gif
[5] => myimages
)
Array
(
[0] => myimages
[1] => horse.gif
[2] => dog.gif
[3] => cat.gif
[4] => ..
[5] => .
)
Definition and Usage
The scandir() function returns an array of files and directories in the specified directory.
Syntax
scandir(directory,sorting_order,context);
| Parameters | Description |
|---|---|
| directory | Required. Specifies the directory to be scanned. |
| sorting_order | Optional. Specifies the sorting order. Default is 0, which means sort alphabetically in ascending order. If set to SCANDIR_SORT_DESCENDING or 1, it sorts alphabetically in descending order. If set to SCANDIR_SORT_NONE, it returns the unsorted result. |
| context | Optional. Specifies the context of the directory handle. context is a set of options that can modify the behavior of the directory stream. |
Technical Details
| Return value: | Returns an array of files and directories on success. Returns FALSE on failure. If directory is not a directory, an E_WARNING level error is thrown. |
|---|---|
| PHP Version: | 5.0+ |
| PHP Changelog: | PHP 5.4: Added the sorting_order constant. |
PHP Directory Reference Manual Other Extensions