Scandir () returns an array of files and directories in the specified directory. returns an array of files and directories if successful. The failure returns FALSE. If directory is not a directory, the e_warning level error is thrown.
Grammar
Scandir (Directory,sorting_order,context);
Parameters |
Description |
Directory |
Necessary. Specifies the directory to scan. |
Sorting_order |
Optional. Specify the order of arrangement. The default is 0, which indicates ascending alphabetical order. If set to Scandir_sort_descending or 1, the letters are sorted in descending order. If set to Scandir_sort_none, the result is not aligned. |
Context |
Optional. The environment that specifies the directory handle. context is a set of options that modify the behavior of a directory stream. |
Example: The code is as follows:
<?phpprint_r (Scandir (' test_directory '));? >
The output is as follows:
Array ([0]=>.[ 1]=>, .... [2]=>1.txt[3]=>2.txt]
In most cases, you only need an array of file lists for that directory, as follows:
Array ([0]=>1.txt[1]=>2.txt)
Generally by excluding "." or ".." An array of items to resolve: The code is as follows:
<?phpfunctionfind_all_files ($dir) { $root = Scandir ($dir); foreach ($rootas $value) { if ($value = = = ". ' | | $value = = = ' ... ') { continue; } if (Is_file ("$dir/$value")) { $result [] = "$dir/$value"; Continue; } foreach (Find_all_files ("$dir/$value") as$value) { $result [] = $value; } } Return$result; }? >
Another method, using the Array_diff function, is to reject the array that the Scandir function performs: The code is as follows:
<?php$directory= '/path/to/my/directory '; $scanned _directory=array_diff (Scandir ($directory), Array ('.. ', '. ')); >
Typically, code management generates an. svn file, or a. htaccess file that restricts directory access. Therefore, it is more convenient to filter by Array_diff function.