problem - list directory files and mp3 id3

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tiagopassos
Forum Newbie
Posts: 1
Joined: Tue Aug 17, 2004 2:11 am

problem - list directory files and mp3 id3

Post by tiagopassos »

hi...
i need a script that list the mp3 files in a dir, and at this list, show the id3 os the mp3 files, exemple:


---
braveheart-children.mp3
song: Children
artist: Hiding Place (ep)
album: Braveheart
year: 2001

manu_chao-proxima_estacion_esperanza.mp3
song; Proxima Estación Esperanza
Artist Manu Chao
Album Proxma Estación Esperanza
Year: 2000
---

i have this code, but it don't function. It shows only the 1st registry. If you want to help, you can edit this code, or suggest me another. Thanks.

Code: Select all

<?php
unset($G['file_list'], $G['pfile_list'], $G['filename']);
    //-------------------------------
    // Build list of uploaded files
    //-------------------------------

    $diretorio = "/var/www/derock/derock/mp3/";
    $dh = opendir($diretorio);
    if ($dh == FALSE) {
        echo "ERROR opening file"; exit;
    }

    $x = 0;
    while (FALSE !== ($file = readdir($dh))) {
        if ("." == $file OR ".." == $file) {
            continue;    // Skip non files
        }
//echo $file."<br/>";
        $file_list[$x] = $file;
        $x++;
    }
    // If we had no files in our list, display a screen error    
    if (0 == $x) {
        echo "There are no files available for processing.";
    } else {
    // If we had some files in our list, sort them and echo the list
        sort($file_list);
        for ($i = 0; $i <= $x; $i++) {
            $arquivos_array[] = "$file_list[$i]";
        }
    } 

// lista o conteudo do array $arquivos_array
    while(list($key, $value) = each($arquivos_array))
    {
        print("<BR><BR>$key: $value<BR>\r\n");    
    

// informa o ID3.V1

       $mp3 = $diretorio . $value;
    
       $file = fopen("$mp3", "r");
       fseek($file, -128, SEEK_END);
    
       $tag = fread($file, 3);
    
    
       if($tag == "TAG")
       {
           $data["song"] = trim(fread($file, 30));
           $data["artist"] = trim(fread($file, 30));
           $data["album"] = trim(fread($file, 30));
           $data["year"] = trim(fread($file, 4));
           $data["comment"] = trim(fread($file, 30));
        
       }
       else
           die("Mp3 does not contain an ID3 tag!");
    
       fclose($file);
    
       while(list($key, $value) = each($data))
       {
          print("$key: $value<BR>\r\n");    
       }

    }

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

1st registry? You mean the first directory? ... If you want it to traverse all subfolders then you need to check if each "file" found is actually a directory. When you find a directory, you need to parse it.. this is often done through recursion, but you have to be careful as you can fill your available memory if run too deep..

In the past, I've run just a directory search first.. that pulls all directories contained under the starting one. Once all those are added to a list, it's sorted, then each of those is parsed for files.
Post Reply