Get the hierarchy of a directory with PHP

2019-01-06 21:25发布

问题:

I'm trying to find all the files and folders under a specified directory

For example I have /home/user/stuff

I want to return

/home/user/stuff/folder1/image1.jpg
/home/user/stuff/folder1/image2.jpg
/home/user/stuff/folder2/subfolder1/image1.jpg
/home/user/stuff/image1.jpg

Hopefully that makes sense!

回答1:

function dir_contents_recursive($dir) {
    // open handler for the directory
    $iter = new DirectoryIterator($dir);

    foreach( $iter as $item ) {
        // make sure you don't try to access the current dir or the parent
        if ($item != '.' && $item != '..') {
            if( $item->isDir() ) {
                // call the function on the folder
                dir_contents_recursive("$dir/$item");
            } else {
                // print files
                echo $dir . "/" .$item->getFilename() . "<br>";
            }
        }
    }
}


回答2:

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
    echo "$f \r\n";   
}


回答3:

The working solution (change with your folder name)

<?php
$path = realpath('yourfolder/subfolder');
## or use like this
## $path = '/home/user/stuff/folder1';

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
        echo "$filename\n";
}
?>


回答4:

Use scandir()



回答5:

$dir = "/home/user/stuff/";
$scan = scandir($dir);

foreach ($scan as $output) {
    echo "$output" . "<br />";
}


回答6:

listAllFiles( '../cooktail/' ); //send directory path to get the all files and floder of root dir

function listAllFiles( $strDir ) {

    $dir = new DirectoryIterator( $strDir );

    foreach( $dir as $fileinfo ) {
        if( $fileinfo == '.' || $fileinfo == '..' ) continue;

        if( $fileinfo->isDir() ) {
            listAllFiles( "$strDir/$fileinfo" );
        }

        echo $fileinfo->getFilename() . "<br/>";
    }
}


回答7:

Beside RecursiveDirectoryIterator solution there is also glob() solution:

// do some extra filtering here, if necessary
function recurse( $item ) {
    return is_dir( $item ) ? array_map( 'recurse', glob( "$item/*" ) ) : $item;
};

// array_walk_recursive: any key that holds an array will not be passed to the function. 
array_walk_recursive( ( recurse( 'home/user/stuff' ) ), function( $item ) { print_r( $item ); } );


回答8:

You can use the RecursiveDirectoryIterator or even the glob function. Alternatively, the scandir function will do the job.



回答9:

Find all the files and folders under a specified directory.

function getDirRecursive($dir, &$output = []) {
    $scandir = scandir($dir);

    foreach ($scandir as $a => $name) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $name);
        if (!is_dir($path)) {
            $output[] = $path;
        } else if ($name != "." && $name != "..") {
            getDirRecursive($path, $output);
            $output[] = $path;
        }
    }

    return $output;
}

var_dump(getDirRecursive('/home/user/stuff'));

Output (example) :

array (size=4)
  0 => string '/home/user/stuff/folder1/image1.jpg' (length=35)
  1 => string '/home/user/stuff/folder1/image2.jpg' (length=35)
  2 => string '/home/user/stuff/folder2/subfolder1/image1.jpg' (length=46)
  3 => string '/home/user/stuff/image1.jpg' (length=27)