Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using PHP to get the ID3 tags of all the mp3 files in a folder

Status
Not open for further replies.

shaiss

IS-IT--Management
Mar 26, 2004
16
0
0
US
Trying to use id3 code found on which worked fine, but I'm trying to step through each file in a directory and get the id3 info


Here's my code
Code:
<HTML>
<HEAD>
 <TITLE>MP3 Playlist</TITLE>
</HEAD>
<BODY>
<?php

//Declarations
$dir = "/private/var/root/Media/iTunes_Control/Music/F00";
//$mp3 = "/private/var/root/Media/iTunes_Control/Music/F00/YSDN.mp3"; //MP3 file to be read

                 //make a array of genres
    $genre_arr = array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge",
"Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B",
"Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska",
"Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient",
"Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance","Classical",
"Instrumental","Acid","House","Game","Sound Clip","Gospel",
"Noise","AlternRock","Bass","Soul","Punk","Space","Meditative",
"Instrumental Pop","Instrumental Rock","Ethnic","Gothic",
"Darkwave","Techno-Industrial","Electronic","Pop-Folk",
"Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta",
"Top 40","Christian Rap","Pop/Funk","Jungle","Native American",
"Cabaret","New Wave","Psychadelic","Rave","Showtunes","Trailer",
"Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro",
"Musical","Rock & Roll","Hard Rock","Folk","Folk-Rock",
"National Folk","Swing","Fast Fusion","Bebob","Latin","Revival",
"Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock",
"Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band",
"Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson",
"Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus",
"Porn Groove","Satire","Slow Jam","Club","Tango","Samba",
"Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle",
"Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall");

//$dir = getcwd() . "\n";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($fileread = readdir($dh)) !== false) {
            //show the file name and location
            $fullpath = $dir . $fileread;
            echo "filename: <b>$fullpath</b>";
            echo "<br>";

            //Get the ID3 Tag info
            $musicfile = $fullpath; //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));
            $data["year"] = trim(fread($musicfile, 4));
            $data["comment"] = trim(fread($musicfile, 30));
            $data["genre"] = $genre_arr[ord(trim(fread($musicfile, 1)))];
            }
            else
                    die("MP3 file does not have any ID3 tag!");

            fclose($musicfile);

            while(list($key, $value) = each($data))
                {
                print("$key: $value<br>\r\n");
                }
        }
    }
}
//echo getcwd() . "\n";
//echo $sCwd = (substr(PHP_OS, 0, 3) == 'WIN') ? strtolower(getcwd()) : getcwd();
?>
</BODY>
</HTML>

when you try to run it you get the following error:
Code:
filename: /private/var/root/Media/iTunes_Control/Music/F00.

Warning: fseek(): supplied argument is not a valid stream resource in /private/var/root/Sites/getid3.php on line 49

Warning: fread(): supplied argument is not a valid stream resource in /private/var/root/Sites/getid3.php on line 51
MP3 file does not have any ID3 tag!

any ideas why?
 
Yes, because fseek and fread both expect a handle as opposed to a path in string format.

You have commented out the fopen portion, that would produce the handle they expect.

Code:
 $musicfile = $fullpath; //[red]fopen($fullpath, "r");[/red]
            fseek($musicfile, -128, SEEK_END);
            
            $tag = fread($musicfile, 3);

Try:

Code:
$musicfile=fopen($fullpath,"r");



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top