Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP scandir() Function

PHP scandir() Function

PHP Directory PHP Directory Reference Manual

Example

List the files and directories in the images directory:

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] => .

)


Definition and Usage

The scandir() function returns an array of files and directories in a specified directory.


Syntax

scandir(

directory,sorting_order,context

);

Parameters Description
directory Required. Specifies the directory to scan.
sorting_order Optional. Specifies the sort order. Default is 0, which sorts alphabetically in ascending order. If set to SCANDIR_SORT_DESCENDING or 1, it sorts alphabetically in descending order. If set to SCANDIR_SORT_NONE, the result is returned unsorted.
context Optional. Specifies the context of the directory handle. A context is a set of options that can modify the behavior of a directory stream.

Technical Details

Return Value: Returns an array of files and directories on success, or FALSE on failure. If directory is not a directory, an E_WARNING level error is raised.
PHP Version: 5.0+
PHP Changelog: PHP 5.4: The sorting_order constants were added.

PHP Directory PHP Directory Reference Manual