3

i just got involved in php stuffs , execuse me for any confusing terms

I'am using the code below to display files and directories on the current path :

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff); //METHOD To List Files In Directory
            echo '</li>';
        }
    }
    echo '</ol>';
}

i'am listing it as a link echo '<li><a href="">'.$ff.'</a>';

i want to be able to take the name of the directories clicked on and enter it display anyhting on it :

  function List($DirectoryName){
      if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
     }

How can i manage to do that and call this function after a directory is clicked on

Huster
  • 329
  • 2
  • 3
  • 12

1 Answers1

0

You have the path, so just url encode it and pass it in the query string href='?dir=$folder'

Check if it's set, then use it, else some default.

<?php
function listFolderFiles($dir){
    // validate or do nothing
    if(is_dir($dir)){
        $ffs = scandir($dir);
        echo '<ol>';
        foreach($ffs as $ff){
            if($ff != '.' && $ff != '..'){
                echo '<li>';

                // conditionally echo link if directory, else just filename
                if(is_dir($dir.'/'.$ff)){
                    $folder = rawurlencode("$dir/$ff");
                    echo "<a href='?dir=$folder'>$ff</a>";

                    // recurse
                    listFolderFiles($dir.'/'.$ff);
                } else {
                    echo $ff;
                }
                echo '</li>';
            }
        }
        echo '</ol>';
    }
}

if(isset($_GET['dir'])){
    $folder = $_GET['dir'];
} else {
    $folder = __DIR__;
}

listFolderFiles($folder);
Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167