简体   繁体   中英

output files from multiple folders

is there a way to output a folder and its files like this using php

this is my directories

folder/12345/file1.php

folder/46745/file1.php

folder/57756/file1.php

i tried this...

$a = glob("folder/*");

foreach ($a as $key) {

    echo $key."<br>";

}

but the output will be like this

folder/12345

folder/46745

folder/57756

i am trying to make output to be more like...

folder/12345

file.php

folder/46745

file1.php

folder/57756

file.php

my point is how many file is inside a folder should be outputted below the folder. hope some one help me with this. thanks

You want scandir: https://www.php.net/manual/en/function.scandir.php

scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] ) : array

Returns an array of filenames on success, or FALSE on failure.

Example:

$dirArr = ['folder/12345', 'folder/46745', 'folder/57756'];

foreach($dirArr as $dir){
    $fileArr = scandir($dir);
    echo $dir.'\r\n';
    print_r($fileArr);
    echo '\r\n';
}

Result:

folder/12345
file1.php
file2.php

folder/46745
file1.php

folder/57756
file1.php
file2.php
file3.php

Get the directories, loop them, get the files and their basename and implode on <br> :

foreach(glob("folder/*", GLOB_ONLYDIR) as $dir) {
    echo "$dir<br>";
    echo implode("<br>", array_map("basename", glob("$dir/*"))) . "<br>";
}

Or have a look at the RecursiveDirectoryIterator class .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM