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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.