Jump to content

Echo recursive list of directories and files


Guest kilbad

Recommended Posts

Guest kilbad
I have some mp3's on my webserver and they are in a "music" directory.  Below that dir, I have separate directories named after each album, which then contain the mp3's from each album, named after each song.  Here is the question, I want to make a script that outputs a list of the "music" directory's subdirectories, and below each of them, a list of all the song files in those respective dirs (minus the mp3 file extention).  The output would look like..

Example Album 1
    Song 1 (link)
    Song 2 (link)
Example Album 2
    Song 1 (link)
    Song 2 (link)

Also, I want to mention that this would NOT be a public list, but for my use only (to ease any copyright issues you might be thinking about).

So can someone direct me in doing this?

Thank you so much!  Brendan
Guest kilbad
so here is my script.. which works..  however, how do I get this to output directories in Alphabetical order AND strip off the .mp3 file extention when it echos??

thanks again!

[code]
<?php

function getDirectory( $path = './music', $level = 0 ){

    $ignore = array( 'cgi-bin', '.', '..' );
    // Directories to ignore when listing output. Many hosts
    // will deny PHP access to the cgi-bin.

    $dh = @opendir( $path );
    // Open the directory to the handle $dh
   
    while( false !== ( $file = readdir( $dh ) ) ){
    // Loop through the directory
   
        if( !in_array( $file, $ignore ) ){
        // Check that this file is not to be ignored
           
            $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
            // Just to add spacing to the list, to better
            // show the directory tree.
           
            if( is_dir( "$path/$file" ) ){
            // Its a directory, so we need to keep reading down...
           
                echo "$spaces $file<br />";
                getDirectory( "$path/$file", ($level+1) );
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
           
            } else {
           
                echo "$spaces <a href='$path/$file'>$file</a><br />";
                // Just print out the filename
           
            }
       
        }
   
    }
   
    closedir( $dh );
    // Close the directory handle

}

[/code]
To get rid of the .mp3

[code]echo "$spaces <a href='$path/$file'>" . str_replace(".mp3","",$file) . "</a><br />";[/code]

Directories are listed in the same order as if you did "dir" or "ls -l", they may seem to be out of order do to capital and lower case initial letters being treated differently.
Guest kilbad
First, thank you all for your help so far!

Basically, the only thing I have left to figure out is how to get this list alphabetized.  In the ftp program I use, and in putty, the music dir on my server (when I use ls and ls -l) is alphabetical in order.  However, when run the script, it produces a list that is alphabetical except for the "Donald Trump" reference which is a dir I made several days after the others.  See http://kilbad.com/test.php for the rough draft output (I realize the .htaccess is showing).

However, thinking it was arranging the dir by time stamp, I edited the Bjork dir today to see if it would then go to the bottom of the list with the new timestamp, but it did not.

any ideas?  thanks again, brendan
inside your readdir loop, instead of printing your output immediately, just put it in an array, like this:

[code=php:0]$myarray[$i++]==$file;[/code]

then you have a $myarray full of filenames. [code=php:0]arsort($myarray)[/code] will then sort the array alphabetically.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.