Guest kilbad Posted August 24, 2006 Share Posted August 24, 2006 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 https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/ Share on other sites More sharing options...
ronverdonk Posted August 24, 2006 Share Posted August 24, 2006 Have a look at the snippet at the CodingForums.com, link[url=http://www.codingforums.com/showthread.php?t=71882]http://www.codingforums.com/showthread.php?t=71882[/url]Ronald 8) Link to comment https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-79964 Share on other sites More sharing options...
michaellunsford Posted August 24, 2006 Share Posted August 24, 2006 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 Link to comment https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-79967 Share on other sites More sharing options...
Guest kilbad Posted August 25, 2006 Share Posted August 25, 2006 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]<?phpfunction 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( ' ', ( $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 https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-80134 Share on other sites More sharing options...
hitman6003 Posted August 25, 2006 Share Posted August 25, 2006 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 https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-80138 Share on other sites More sharing options...
Guest kilbad Posted August 25, 2006 Share Posted August 25, 2006 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 https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-80373 Share on other sites More sharing options...
michaellunsford Posted August 25, 2006 Share Posted August 25, 2006 You might have to put the entire thing in a big array, then do an asort($array) Link to comment https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-80380 Share on other sites More sharing options...
Guest kilbad Posted August 25, 2006 Share Posted August 25, 2006 I am not sure how I would put the entire thing in an array? Link to comment https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-80385 Share on other sites More sharing options...
michaellunsford Posted August 25, 2006 Share Posted August 25, 2006 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. Link to comment https://forums.phpfreaks.com/topic/18564-echo-recursive-list-of-directories-and-files/#findComment-80432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.