• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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