1

Good evening all,

I'm really struggling to find a way to list the directories of a given folder and also the subdirectories without adding in the files, as well as assigning them to a variable.

I've tried using getcwd, global, GLOB_ONLYDIR scandir, but can't get it to work out.

Is there anyway to assign each folder only to a variable?

$currentFolder2 =  getcwd();

foreach (glob($currentFolder2 . '/*' , GLOB_ONLYDIR) as $dir) {
    $files = glob($dir . '/*');
    echo '<pre>'; print_r($files);
    $link[0] = $files;
    foreach (glob($files . '/*' , GLOB_ONLYDIR) as $dir) {
        $subfiles = glob($dir . '/*');
        echo '<pre>'; print_r($subfiles);
        $sublink[0] = $files;
    }
}

What I need to produce is folder links:

-Link to Folder1
  -Link to Folder1-SubFolder1
  -Link to Folder1-SubFolder2
-Link to Folder2
  -Link to Folder2-SubFolder1
  -Link to Folder2-SubFolder2
-Link to Folder2
  -Link to Folder2-SubFolder1
  -Link to Folder2-SubFolder2

It's not laying out the way I planned and it's also returning html pages for some reason.

Have I over complicated the way I am going about it or have I gone completely the wrong way.

Thanks in advance

4
  • 1
    This question is similar to: How to efficiently use DirectoryIterator?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 7 at 17:41
  • To append values to an array, do not use the index. $link[] = $files; Commented Jul 7 at 17:42
  • $files . '/*' makes no sense. $files is an array, not a string, you can't concatenate it.
    – Barmar
    Commented Jul 7 at 19:14
  • What you probably want is foreach ($files as $subdir) { $subfiles = glob("$subdir/*", GLOB_ONLYDIR); ... }
    – Barmar
    Commented Jul 7 at 19:17

1 Answer 1

-1

The logic:

  1. Scan all files and folders with scandir() that gives you an array of everything
  2. Check each object if it is directory with is_dir() function an if so, process it
  3. Scan current directory for step 1 and 2 that will proccess sub directories

You need 2 scandir(): one for root and one for each parent folder

Sample code:

$content = ''; //For collecting future <li>'s
$folders = array_diff(scandir(ROOT_PATH, 1), array('.', '..', '.ftpquota', '.htaccess', 'index.html', 'index.php', 'error_log'));
if ($folders) {
    foreach ($folders as $folder) {
        if (is_dir(PATH_TO_$folder)) {
            $content .= '<li class="parent">'.$folder.'</li>'; //Add to any html you like
            
            //Now, scan this folder for subfolders:
            $subfolders = array_diff(scandir(ROOT_PATH.'/'.$folder, 1), array('.', '..', '.ftpquota', '.htaccess', 'index.html', 'index.php', 'error_log'));
            if ($subfolders) {
                foreach ($subfolders as $subfolder) {
                    if (is_dir(PATH_TO_$subfolder)) {
                        $content .= '<li class="child">'.$subfolder.'</li>'; //Add to any html you like
                    }
                }
            }
        }
    }
}

Check it

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 8 at 12:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.