Click to See Complete Forum and Search --> : PHP - Read Nested Folders and Files
andrthad
08-29-2003, 05:23 PM
Hi All,
I am a PHP newbie. Say you have the following file structure:
Counting Crows (artist)
Hard Candy (album)
Hard Candy.mp3 (song name)
American Girls.mp3
David Gray
White Ladder
Please Forgive Me.mp3
White Ladder.mp3
I need some suggestions on how to read this file structure either into an array or possibly just to update a MySQL database and then read the database. I will constantly be adding or subtracting files so I want to try and make it as dynamic as I can.
Suggestions, comments, code?
Musicman
08-29-2003, 06:00 PM
Hi,
just a suggestion: if you have mysql available, use it....
You can avoid one level of folders if you want, and, more important, can modify file and folder names to avoid whitespace, slashes, & signs or other characters that might cause problems
As for structuring the database: usually there may be several albums by the same artist, so I could imagine to have a separate artist table, an album table referencing the artists, and a song table referencing the albums
Musicman
andrthad
08-29-2003, 06:40 PM
Hi musicman,
I was hoping I would get you. You helped me in the past with a PHP that actually read the mp3 file names from within a folder. I was thinking that a music player that played files from a mysql database would be sweet, however, I would want the values in the database to be updated directly from the folder structure on your webserver.
For instance like you said have an artist table, an album table, and a song name table. Have an admin php script that reads the first layer of folders and understands that these are the artists names that should appear in the artist table name. Then reads the next layer of folders and interprets that to be all the artist's names. Lastly the third level down would be all the mp3s. I actually have the php script for that.
I'm starting to see the layout. Build the tables in mysql. Upload mp3 files with correct folder structure (artist / album / song.mp3). Build a php admin page to update tables with currect web folder structure. Enable the flash music player to read the mysql database. What do you think?
Musicman
08-29-2003, 06:54 PM
Hi,
sounds reasonable - now it is up to you to decide how the player will interact with the database, e.g.
- tree structure
- search by artist / by album / by song
- advanced search
- search keyword in all columns
Musicman
andrthad
08-31-2003, 12:45 PM
Musicman,
You taugth me how to get files within a certain directory:
<?
$n = 0;
$dp = opendir("musicPopular") or die("status=fail");
while($f = readdir($dp))
{
if($f != "." && $f != ".."){
if($n == 0){
print "im$n=$f";
}
else{
print "&im$n=$f";
}
$n++;
}
}
print "&total=$n";
?>
How would you get only folder names within a certain directory?
Musicman
08-31-2003, 02:05 PM
Hi,
useful php functions is_file() and is_dir() ...
note: the code should be something like
$dp = opendir("somedir");
while($f = readdir($dp))
{ if(is_dir("somedir/$f")) ...
Without putting the somedir into the test, the script would test whether a name found in somedir exists as a directory within the current dir
Musicman
flashkit.com
Copyright WebMediaBrands Inc., All Rights Reserved.