The scandir() function returns an array containing files and directories in the specified path. As shown below:
Example:
Copy code The code is as follows:
print_r(scandir('test_directory'));
? >
Output:
Copy code The code is as follows:
Array
(
[0]=>.
[1] =>..
[2]=>1.txt
[3]=>2.txt
)
In most cases, you only need the file list array of the directory , as follows:
Copy code The code is as follows:
Array
(
[0]=>1.txt
[ 1]=>2.txt
)
is usually solved by excluding "." or ".." array items:
Copy code The code is as follows:
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 is to use the array_diff function to eliminate the array obtained by executing the scandir function:
Copy code The code is as follows:
$directory='/path/to/my/directory';
$scanned_directory=array_diff(scandir($directory),array('..','.'));
?>
Usually code management will generate .svn files, or Files such as .htaccess that restrict directory access permissions. So it is more convenient to filter through the array_diff function.
http://www.bkjia.com/PHPjc/788621.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/788621.htmlTechArticlescandir() function returns an array containing files and directories in the specified path. As shown below: Example: Copy the code The code is as follows: ?php print_r(scandir('test_directory')); ? Enter...