为了循环目录中的所有文件,我在php中创建了这段代码:
$dir = new DirectoryIterator(dirname(__FILE__));
$files = scandir($dir.'/');
foreach($files as $file)
{
echo $file;
echo "\n";
}
但是我无法列出目录的多个子目录中的所有文件。
答案 0 :(得分:5)
使用RecursiveDirectoryIterator
<?php
$path = realpath('/etc');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
echo "$name\n";
}
?>
答案 1 :(得分:0)
试试这个。 See this in php manual
function loop_dir($dir) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $path) {
echo $path
}
}
答案 2 :(得分:0)
这很简单:
$dir = dir('DIRECTORY_TO_DISPLAY_HERE');
while (($file = $dir->read()) !== false) {
if ($file != '..' && $file != '.') {
echo $file;
}
}
$dir->close();