任何人都可以告诉我如何阅读目录并查找该目录中的文件和目录。
我尝试使用is_dir()
函数检查目录,如下所示
$main = "path to the directory";//Directory which is having some files and one directory
readDir($main);
function readDir($main) {
$dirHandle = opendir($main);
while ($file = readdir($dirHandle)) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
//nothing is coming here
}
}
}
}
但它没有检查目录。
由于
答案 0 :(得分:1)
您必须使用子路径的完整路径:
if(is_dir($main.'/'.$file)) { ... }
答案 1 :(得分:1)
PHP 5中最简单的方法是使用RecursiveDirectoryIterator
和RecursiveIteratorIterator
:
$dir = '/path/to/dir';
$directoryIterator = new RecursiveDirectoryIterator($dir);
$iterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $path) {
if ($path->isDir()) {
// ...
}
}
你不需要自己递归,因为这些精细的迭代器会自己处理它。
有关这些强大迭代器的更多信息,请参阅链接的文档文章。
答案 2 :(得分:0)
使用scandir
然后解析结果并消除'。'和'..'和is_file()
答案 3 :(得分:0)
$le_map_to_search = $main;
$data_to_use_maps[] = array();
$data_to_use_maps = read_dir($le_map_to_search, 'dir');
$aantal_mappen = count($data_to_use_maps);
$counter_mappen = 1;
while($counter_mappen < $aantal_mappen){
echo $data_to_use_maps[$counter_mappen];
$counter_mappen++;
}
$data_to_use_files[] = array();
$data_to_use_files = read_dir($le_map_to_search, 'files');
$aantal_bestanden = count($data_to_use_files);
$counter_files = 1;
while($counter_files < $aantal_files){
echo $data_to_use_files [$counter_files ];
$counter_files ++;
}
答案 4 :(得分:0)
答案 5 :(得分:0)
试试这个
$handle = opendir($directory); //Open the directory
while (false !== ($file = readdir($handle))) {
$filepath = $directory.DS.$file; //Get all files/directories in the directory
}