Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

php - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.'
';
    print_r($fileArr);
    echo '
';
}

Result:

folder/12345
file1.php
file2.php

folder/46745
file1.php

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...