I'm writing a script that goes through a folder and gets all the ID3 tags for all the MP3 files in that folder. This is my code:

<?php

$dir = TEMPLATEPATH . "/flash/music/";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($fileread = readdir($dh)) !== false) {
			if($fileread == "." || $fileread == ".." || $fileread == "index.php" ) 
        	continue; 

        $fullpath = $dir . $fileread;

        $musicfile =  fopen($fullpath, 'r');
        fseek($musicfile, -128, SEEK_END);


        $tag = fread($musicfile, 3);

        if($tag == "TAG")
        {
        	$data["song"] = trim(fread($musicfile, 30));
        	$data["artist"] = trim(fread($musicfile, 30));
        	$data["album"] = trim(fread($musicfile, 30));
        } 
        else
        {
            echo "MP3 file does not have any ID3 tag!";

        }

        fclose($musicfile);

        while(list($key, $value) = each($data))
        {
            print("$key: $value<br>\r\n");
        }
    }
}
}

?>

but its not quite working right. It's returning this:
MP3 file does not have any ID3 tag!
Warning: Variable passed to each() is not an array or object in /[my_server_path]/public_html/new/wp-content/themes/default/functions.php on line 1053
song: X&Y
artist: Coldplay
album: X&Y

So oddly enough it only retrieves the second song's id3 data.
Any ideas?
Thanks.

    Are you sure all the MP3s have an ID3v1 tag?
    http://en.wikipedia.org/wiki/ID3

    You'll see there are two forms and your code is only really reading the first, but I'd say the second is the most common since they aren't limited to 30 chars for each.

      I am sure. I actually figured that part out. Here is my code if anybody in the future is interested.

      <?php 
      	if ($dir = opendir( "flash/music/" )) {  
      $files = array(); while (false !== ($file = readdir($dir))) {
      $files[] = $file; }
      sort($files); foreach($files as $file) { if ($file != "." && $file != "..") {
      $myfile = "flash/music/" .$file; $musicfile = fopen($myfile, 'r'); fseek($musicfile, -128, SEEK_END); $tag = fread($musicfile, 3); if($tag == "TAG") { $data["song"] = trim(fread($musicfile, 30)); $data["artist"] = trim(fread($musicfile, 30)); $data["album"] = trim(fread($musicfile, 30)); } echo "<tr>"; echo "<td>" . $data["song"] . "</td>"; echo "<td>" . $data["artist"] . "</td>"; echo "<td>" . $data["album"] . "</td>"; fclose($musicfile); echo "<td><a href=\"flash/music/".rawurlencode($file)."\">$file</a></td>"; echo "<td> <input type=\"hidden\" name=\"filename\" value=\"$file\"> <input type=\"button\" value=\"Delete\"> </td> "; echo "</tr>"; } } } ?>

      I did have another issue though. I will start a new thread for it though.

        Write a Reply...