1

Hi I'm a disciple seeking help from the Masters. I wanted to navigate the folders using PHP here's my code:

foreach (glob ("folderswithsubfoldersandfilesinit") as $path) {
  $file = basename($path);
  echo '<ul><a href="'.$path.'">'.$file.'</a></ul>';
}

The code above will display the files inside the specified folder. What if some items in it is a subfolder? What code will I add if I want to click the subfolder and see the files in it? I really need help thanks.

Dax Vilbar
  • 19
  • 6

3 Answers3

1

Use scandir instead of glob

foreach (scandir("folderswithsubfoldersandfilesinit") as $path) {
$file = basename($path);
echo '<ul><a href="'.$path.'">'.$file.'</a></ul>';
}
Nagaraja K
  • 46
  • 3
0

use is_dir php function

$dirPath = '/your/start/directory';
if(isset($_GET['dir']) {
    $dirPath = $_GET['dir'];
}
foreach (glob ($dirPath) as $path) {
  $file = basename($path);
  if(is_dir($path)) {
     echo "<a href='?dir=$path'>$file</a>";
  } else {
     echo 'file: ' . $file;
  }
}

I think you can replace echo directory and echo file to desired html code, for processing you file. p.s. I can't write full code for you, because I think stackoverflow for learning ad help begining developers.

Naumov
  • 1,167
  • 9
  • 22
  • I still don't get it.. Can you please give me an example where it is listed as links? If I click the file it will download the file. If I will click the subfolder it will list its files. Thanks – Dax Vilbar Oct 08 '18 at 11:31
  • After I tested your code it still displays the index of after I clicked a subfolder. – Dax Vilbar Oct 08 '18 at 11:44
  • I understand thank you for your effort. I hope someone will have the answer to my question. – Dax Vilbar Oct 08 '18 at 12:46
0

recursive function means a function that calls itself and this is the correct way of parsing a tree structure like a directory.
This is an example:

const ROOT_DIR = "C:\\mydir";

print_files_of_dir( ROOT_DIR );

function print_files_of_dir( $dir ) {
    $files_and_dirs = scandir( $dir );

    foreach( $files_and_dirs as  $file_or_dir ) {
        if ( $file_or_dir === '.' || $file_or_dir === '..' ) {
            continue;
        }

        $full_path = $dir . DIRECTORY_SEPARATOR . $file_or_dir;

        if ( is_dir( $full_path ) ) {
            print_files_of_dir( $full_path );
        }
        else {
            echo $full_path . '<br>';
        }
    }
}

You can see that the function print_files_of_dir prints all files of a dir and when it finds a subdirectory it calls itself for that subdirectory.
This way the entire tree is resolved.
I hope it solves your problem.
Giancarlo

  • Thank you for that clear and precise explanation of recursion. All that I wanted to happen is to list the files and folders in the specified folder and treat each items as links.. If it is a file it will download. If it is a subfolder it will list the files inside the subfolder. fileA.doc <-- If I will click this one it will download. This one is working fileB.jpg Subfolder < -- If I click this one it will display all files inside of it. I don't know how fileC.mdb – Dax Vilbar Oct 08 '18 at 13:13
  • My code lists all files under `ROOT_DIR`. You can use it as a starting point and replace the echo instructions with your own logic. – Giancarlo Mosso Oct 09 '18 at 05:46