The Scandir () function returns an array that contains the files and directories in the specified path. As shown below:
Example:
Copy Code code as follows:
<?php
Print_r (Scandir (' test_directory '));
?>
Output:
Copy Code code as follows:
Array
(
[0]=>.
[1]=>
[2]=>1.txt
[3]=>2.txt
)
In most cases, you only need an array of file lists for this directory, as follows:
Copy Code code as follows:
Array
(
[0]=>1.txt
[1]=>2.txt
)
Generally by excluding "." or ".." Of the array items that are resolved by:
Copy Code code as follows:
<?php
Functionfind_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, eliminates the array of scandir functions performed:
Copy Code code 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. So it is more convenient to filter by Array_diff function.