Search code examples
phphtmlwebmp3id3

Read/Add ID3 Tags from .mp3s to HTML using PHP


After doing some research, I found this link to a "guide" on PHP: ID3 Functions. I'm not quite sure how to go about implementing this into a website so that:

  • Finds all .mp3's in a folder
  • Reads the ID3 tags from the file (Artist, year, etc.)
  • Allows me to put the ID3 tags along with a link to the file on an HTML website

I already have the finding all .mp3's in a folder working, but I'm not sure how to implement the ID3 functions to read from those files.


Solution

  • The ID3 package is long abandoned and no longer shipped with PHP 7 and up.
    Check out getID3 for an alternative.


    PHP 5:

    Easiest way would probably be id3_get_tag($filename, $version).

    Just provide a string containing the file path and optionally a ID3 Tag version (leave out if you don't know):

    $files = array('/folder/file1.mp3', '/folder/file2.mp3');
    $tags = array();
    foreach($files as $file) {
        $tags[$file] = id3_get_tag($file);
        // convert genre:
        if(array_key_exists('genre', $tags[$file]) && is_integer($tags[$file]['genre'])
           && $tags[$file]['genre'] >= 0 && $tags[$file]['genre'] <= 147) {
            $tags[$file]['genre'] = id3_get_genre_name($tags[$file]['genre']);
        }
    }
    

    $tags will then be an array like this:

    ['/folder/file1.mp3'] = array('interpret' => 'John Doe', 'title' => 'PHP is cool', 'genre' => 'Techno')
    ['/folder/file2.mp3'] = array('interpret' => 'Jane Doe', 'title' => 'ASP is low', 'genre' => 'House')
    

    What keys you actually get from this, depends on the ID3 version of each .mp3 file and the actual content (whatever someone wrote into that file).