如何通过php正确显示目录和子目录中的所有文件

我有 4 个文件夹,我想检查其中的图像和 php 文件。这是结构文件夹安排。


该main_folder包含两个子文件夹(second_folder和third_folder) ,而third_folder包含子文件夹(fourth_folder)等。


目前使用下面的代码,我只能访问first_main_folder 中的所有文件。


$files = glob('first_Main_folder/*.{jpg,png,gif,php}', GLOB_BRACE);

foreach($files as $file) {

echo "<br>$file<br>";

}

请问如何访问剩余子文件夹(second_folder、third_folder、fourth_folder)中的文件等等


谢谢


first_Main_folder

test.png

data.php

check.gif



    second_folder

    tony.jpg

    mark.gif

    action.php


    third_folder

    index.php

    post.php

    sender.php

    han.gif


        fourth_folder

            catch.php

            index2.php

            redirect.php


       and so on 


元芳怎么了
浏览 176回答 2
2回答

BIG阳

下面的这个函数将主目录和所有子目录都扫描到最后。这个对我有用。谢谢。function outputFiles1($path){&nbsp; &nbsp; // Check directory exists or not&nbsp; &nbsp; if(file_exists($path) && is_dir($path)){&nbsp; &nbsp; &nbsp; &nbsp; // Search the files in this directory&nbsp; &nbsp; &nbsp; &nbsp; $files = glob($path ."/*");&nbsp; &nbsp; &nbsp; &nbsp; if(count($files) > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through retuned array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($files as $file){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(is_file("$file")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Display only filename&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo basename($file) . filesize($file) . "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if(is_dir("$file")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Recursively call the function if directories found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputFiles1("$file");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "ERROR: No such file found in the directory.";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "ERROR: The directory does not exist.";&nbsp; &nbsp; }}// Call the functionoutputFiles1("C:/xampp/htdocs/first_main_folder");

qq_遁去的一_1

下面的例子取自这篇文章。function rsearch($folder, $pattern='/.*/') {&nbsp; &nbsp; $dir = new RecursiveDirectoryIterator($folder);&nbsp; &nbsp; $ite = new RecursiveIteratorIterator($dir);&nbsp; &nbsp; $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);&nbsp; &nbsp; $fileList = array();&nbsp; &nbsp; foreach($files as $file) {&nbsp; &nbsp; &nbsp; &nbsp; $fileList = array_merge($fileList, $file);&nbsp; &nbsp; }&nbsp; &nbsp; return $fileList;}$result = rsearch('./');print_r($result);
打开App,查看更多内容
随时随地看视频慕课网APP