List files with certain id3 tags

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
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

List files with certain id3 tags

Post by toasty2 »

I am wanting to list mp3's in a directory, but ONLY if the artist is System of a Down, it reads the files' id3 tags, but I obviously messed up somewhere., since It displays a blank page.

Code: Select all

<?php

$path = "/homepages/21/d155481990/htdocs/server";

$dir_handle = @opendir($path) or die("Unable to open $path");

while ($file = readdir($dir_handle))
{
   if($file!="." && $file!=".." && $file!="songlist.php" && $file!="song.php" && $file!="music.html" && $file!="songdevlist.php" && $file!=null)


    $mp3 = $file; //MP3 file to be read


    $filesize = filesize($mp3);
    $file = fopen($mp3, "r");
    fseek($file, -128, SEEK_END);
   
    $tag = fread($file, 3);
   
   
    if($tag == "TAG")
    {
        $data["artist"] = trim(fread($file, 30));   
    }
    else
        die("");
   
    fclose($file);
        strtolower($mp3);
    while(list($key, $value) = each($data))
    {
       
        if ($data["artist"] == 'system of a down')
                        { print("$key: $value<br>\r\n"); }
       
    }
}
//closing the directory
closedir($dir_handle);
?>

And, I have several songs for that artist, and they are in the same directory and I am sure the songs have that artist in their id3 tags. I have a different script that will display the id3 tags of a specific file to verify it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Do these files use ID3v1 or ID3v2 or both? Btw.. you're missing some braces. They shouldn't cause a blank page (which you should check your errors logs for) but would mess up some of the logic you are trying to accomplish.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

This would cause a parse error I believe..

Code: Select all

if ($tag == "TAG")
    {
        $data["artist"] = trim(fread($file, 30));   
    }
    else
        die("");
Change to...

Code: Select all

if ($tag == "TAG")
    {
        $data["artist"] = trim(fread($file, 30));   
    } else 
    {
        die(""); 
    }
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I fixed the possible error, and I also put an error message in the die function, and now it always displays the error message. So, it's not reading the directory's files, at least not correctly.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

feyd wrote:Do these files use ID3v1 or ID3v2 or both?
They use possibly both, I haven't checked, but I'm not trying to get any extra ID tags in the files, just the artist one, which should be in it whether v1 or v2.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your code only works with v1 tags. That's why I've asked.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Well, that code could list each of the files individually in its original form, but I tried to adapt it to its current forum to go through each mp3. If you'd like the original code (which isn't much different), just ask. I also implemented a directory listing script into that also, trying to get the combo to do what I wanted.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it may be better to use strpos() instead of being literal.
Post Reply