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);?>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] => .)The scandir() function returns an array of files and directories in the specified directory.
scandir( directory, sorting_order, context );
parameter | describe |
---|---|
directory | Required. Specifies the directories to be scanned. |
sorting_order | Optional. Specify the order of sorting. The default is 0, indicating ascending alphabetical order. If set to SCANDIR_SORT_DESCENDING or 1, it sorts alphabetically in descending order. If set to SCANDIR_SORT_NONE, unsorted results are returned. |
context | Optional. Specifies the environment for directory handles. context is a set of options that modify the behavior of the directory stream. |
Return value: | On success, an array of files and directories is returned. Returns FALSE on failure. If directory is not a directory, an E_WARNING level error is thrown. |
---|---|
PHP version: | 5.0+ |
PHP change log: | PHP 5.4: New sorting_order constant. |