Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

PHP list folders

I have a piece of script that is incomplete.  What I want to do is list all the folders in a drive.  Be able to click on the folder to display the content of that folder (which is music) and then click on the song so it will play.

This is as far as I have got:

<ul>
<?php
$dir = "D:/Music/";

$files = scandir($dir);
foreach($files as $ind_file){
?>
<li><a href="<?php echo $dir."/".$ind_file;?>"><?php echo $ind_file;?></li>
<?php
}
?>
</ul>

Open in new window

Avatar of maeltar
maeltar
Flag of United Kingdom of Great Britain and Northern Ireland image

This works nice...

<?
    
  function getDirectoryList ($directory) 
  {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

      // if file isn't this directory or its parent, add it to the results
      if ($file != "." && $file != "..") {
        $results[] = $file;
      }

    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

  }
  
  $dir = "D:\K";
  print_r(getDirectoryList($dir));
  ?>

Open in new window

Obviously on line 31 you would do something like

  $dir_list = getDirectoryList($dir);
  
  foreach ( $dir_list as $k=>$v)
  {
      echo "{$v}<br />";
  }

Open in new window

Avatar of Robert Granlund

ASKER

@maeltar:

Thanks this makes a nice list.  However, how can I click on a folder name and have it open and display, in a list, all the files?
ASKER CERTIFIED SOLUTION
Avatar of maeltar
maeltar
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not sure if that is what you are meaning....
Found something that may meet you needs (after reading them correctly)

Demo
http://www.evoluted.net/stuff/

Source
http://www.evoluted.net/thinktank/wp-content/uploads/2011/02/dirlistingv3.zip
That works great.  Now all I have to do is figure out how to link the file so if it is an mp3 it will play.  Any further ideas?
That will be platform dependant, you click the mp3, it will download it and use the local player.

You could use some flash/jQuery to play the files..

http://plugins.jquery.com/plugin-tags/sound