'PHP Jump to folder number

I have this code that lists all the folders in a directory and numbers each folder, but my desire is to be able to go to a specific folder when I want to start, for example:

The code listed all folders after that done, I would like to go to folder 40 how do I get the path of folder number 40 ? I would like to write a file there.

I thought about putting the folders in an array, but there is nothing simpler where I don't need to use an array because it will be a very large directory I will make a repository.

List folder source:

$foldercount = 0;
foreach (glob("folderlist/") as $folder) {
  $foldercount++; 
}
php


Solution 1:[1]

The glob function is already an array, so to access the folder you want just refer to its index number like this:

glob("folderlist/")[2]

It would look something like this:

$folderList  = glob("folderlist/");
$foldercount = 0;
foreach ($folderList) as $folder) {
  $foldercount++; 
}

echo("The last folder is: ".$folderList[$foldercount -1]);

p.s If you only need to list folders I think you would want to use the GLOB_ONLYDIR flag to avoid listing files, like this:

$folderList  = glob("folderlist/", GLOB_ONLYDIR);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jenya.S