Jump to content


Echo recursive list of directories and files


  • Please log in to reply
8 replies to this topic

#1 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 24 August 2006 - 06:02 PM

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


#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 24 August 2006 - 06:21 PM

Have a look at the snippet at the CodingForums.com, link

http://www.codingfor...ead.php?t=71882

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#3 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 24 August 2006 - 06:23 PM

you could use the readdir() command, and check each entry to see if it's a directory using is_dir().

example 2 on the PHP site looks like a good start:
http://us2.php.net/read_dir

#4 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 25 August 2006 - 01:15 AM

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!

<?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

}



#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 25 August 2006 - 01:30 AM

To get rid of the .mp3

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

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.

#6 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 25 August 2006 - 03:44 PM

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

#7 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 25 August 2006 - 03:57 PM

You might have to put the entire thing in a big array, then do an asort($array)

#8 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 25 August 2006 - 04:06 PM

I am not sure how I would put the entire thing in an array?

#9 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 25 August 2006 - 06:42 PM

inside your readdir loop, instead of printing your output immediately, just put it in an array, like this:

$myarray[$i++]==$file;

then you have a $myarray full of filenames.
arsort($myarray)
will then sort the array alphabetically.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users