0

I have some folder structure like this

/photos/1/file1.txt
/photos/2/file2/txt

What i need is to get inside folder photos and get array of all files without path, final array should look like this

$content = ('file1.txt', 'file2.txt');

This is what i have for now, but still not like with final array

function recursive_scandir($dir){
    $contents = array();
    foreach(scandir($dir) as $file){
        if($file == '.' || $file == '..') continue;
        $path = $dir.DIRECTORY_SEPARATOR.$file;
        if(is_dir($path)){
            $contents = array_merge($contents, recursive_scandir($path));
        } else {
            $contents[] = $path;
        }
    }
    return $contents;
}

print_r(recursive_scandir('photos/'));
Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142

1 Answers1

1

You should search better: Get the hierarchy of a directory with PHP

After you get your file with path like "/photos/1/file1.txt", you can use basename.

Community
  • 1
  • 1
Alim Giray Aytar
  • 1,274
  • 1
  • 13
  • 17