PHP Get all subdirectories of a given directory

2019-01-02 17:59发布

How can I get all sub-directories of a given directory without files, .(current directory) or ..(parent directory) and then use each directory in a function?

16条回答
妖精总统
2楼-- · 2019-01-02 18:03

Find all the files and folders under a specified directory.

function getAllSubdir($dir, &$fullDir = []) {
    $currentDir = scandir($dir);

    foreach ($currentDir as $key => $val) {
        $realpath = realpath($dir . DIRECTORY_SEPARATOR . $val);
        if (!is_dir($realpath) && $filename != "." && $filename != "..") {
            getDirRecursive($realpath, $fullDir);
            $fullDir[] = $realpath;
        }
    }

    return $fullDir;
}

var_dump(scanDirAndSubdir('C:/web2.0/'));

Sample :

array (size=4)
  0 => string 'C:/web2.0/config/' (length=17)
  1 => string 'C:/web2.0/js/' (length=13)
  2 => string 'C:/web2.0/mydir/' (length=16)
  3 => string 'C:/web2.0/myfile/' (length=17)
查看更多
还给你的自由
3楼-- · 2019-01-02 18:04
<?php
    /*this will do what you asked for, it only returns the subdirectory names in a given
      path, and you can make hyperlinks and use them:
    */

    $yourStartingPath = "photos\\";
    $iterator = new RecursiveIteratorIterator( 
        new RecursiveDirectoryIterator($yourStartingPath),  
        RecursiveIteratorIterator::SELF_FIRST);

    foreach($iterator as $file) { 
        if($file->isDir()) { 
            $path = strtoupper($file->getRealpath()) ; 
            $path2 = PHP_EOL;
            $path3 = $path.$path2;

            $result = end(explode('/', $path3)); 

            echo "<br />". basename($result );
        } 
    } 

    /* best regards,
        Sanaan Barzinji
        Erbil
    */
?>
查看更多
萌妹纸的霸气范
4楼-- · 2019-01-02 18:05

Try this code:

<?php
$path = '/var/www/html/project/somefolder';

$dirs = array();

// directory handle
$dir = dir($path);

while (false !== ($entry = $dir->read())) {
    if ($entry != '.' && $entry != '..') {
       if (is_dir($path . '/' .$entry)) {
            $dirs[] = $entry; 
       }
    }
}

echo "<pre>"; print_r($dirs); exit;
查看更多
情到深处是孤独
5楼-- · 2019-01-02 18:05

If you're looking for a recursive directory listing solutions. Use below code I hope it should help you.

<?php
/**
 * Function for recursive directory file list search as an array.
 *
 * @param mixed $dir Main Directory Path.
 *
 * @return array
 */
function listFolderFiles($dir)
{
    $fileInfo     = scandir($dir);
    $allFileLists = [];

    foreach ($fileInfo as $folder) {
        if ($folder !== '.' && $folder !== '..') {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $folder) === true) {
                $allFileLists[$folder . '/'] = listFolderFiles($dir . DIRECTORY_SEPARATOR . $folder);
            } else {
                $allFileLists[$folder] = $folder;
            }
        }
    }

    return $allFileLists;
}//end listFolderFiles()


$dir = listFolderFiles('your searching directory path ex:-F:\xampp\htdocs\abc');
echo '<pre>';
print_r($dir);
echo '</pre>'

?>
查看更多
一个人的天荒地老
6楼-- · 2019-01-02 18:07

For the people who actually want folders and subfolders with no files, just like the OP said, the following code outputs both a list of folders and their subfolders, and an array of the same.

<?php
/**
 * Function for recursive directory file list search as an                       array.
 *
 * @param mixed $dir Main Directory Path.
 *
 * @return array
 */
function listFolderFiles($dir)
{
    $fileInfo     = scandir($dir);
    $allFileLists = [];

foreach ($fileInfo as $folder) {
    if ($folder !== '.' && $folder !== '..') {
        if (is_dir($dir . DIRECTORY_SEPARATOR . $folder)     === true) {
            $allFileLists[$folder . '/'] = listFolderFiles($dir .     DIRECTORY_SEPARATOR . $folder);
echo ' '. $folder. ' ' <br>';
            } else {
                echo' ';
            }
        }
    }

    return $allFileLists;
}//end listFolderFiles()

listFolderFiles('C:\wamp64\www\code');
$dir = listFolderFiles('C:\wamp64\www\code');
echo '<pre>';
print_r($dir);
echo '</pre>'

?>
查看更多
不流泪的眼
7楼-- · 2019-01-02 18:09

The following recursive function returns an array with the full list of sub directories

function getSubDirectories($dir)
{
    $subDir = array();
    $directories = array_filter(glob($dir), 'is_dir');
    $subDir = array_merge($subDir, $directories);
    foreach ($directories as $directory) $subDir = array_merge($subDir, getSubDirectories($directory.'/*'));
    return $subDir;
}

Source: https://www.lucidar.me/en/web-dev/how-to-get-subdirectories-in-php/

查看更多
登录 后发表回答