• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

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

0
rgranlund
Asked:
rgranlund
  • 7
  • 2
1 Solution
 
maeltarCommented:
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

0
 
maeltarCommented:
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

0
 
rgranlundAuthor Commented:
@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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
maeltarCommented:
Ahh sorry...

<?

// Set directory path
$dir = "D:/K";
  
  function ListFolder($path)
{
    //using the opendir function
    $dir_handle = @opendir($path) or die("Unable to open $path");
   
    //Leave only the lastest folder name
    $dirname = end(explode("/", $path));
   
    //display the target folder.
    echo ("<li>$dirname\n");
    echo "<ul>\n";
    while (false !== ($file = readdir($dir_handle)))
    {
        if($file!="." && $file!="..")
        {
            if (is_dir($path."/".$file))
            {
                //Display a list of sub folders.
                ListFolder($path."/".$file);
            }
            else
            {
                //Display a list of files.
                echo "<li>$file</li>";
            }
        }
    }
    echo "</ul>\n";
    echo "</li>\n";
   
    //closing the directory
    closedir($dir_handle);
}
  
  
  

 ?> 
  <ul class="dmxtree" id="FolderView">
<?php ListFolder($dir); ?>
</ul>

Open in new window

0
 
maeltarCommented:
Not sure if that is what you are meaning....
0
 
maeltarCommented:
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
0
 
rgranlundAuthor Commented:
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?
0
 
maeltarCommented:
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

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now