Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...