Return to Snippet

Revision: 5633
at March 20, 2008 18:23 by chrisaiv


Initial Code
<?php

       $dir = 'directory_of_mp3_files';
       $file_type = 'mp3';

       $play_list = '<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\'?>';
       $play_list .= "<playlist>";

       // Open directory, read contents and add to file_list if correct file_type
       if (is_dir($dir)) {
          if ($dh = opendir($dir)) {
             while (($file = readdir($dh)) !== false) {
                if ($file != '.' && $file != '..') {
                   $name_array = explode('.', $file);
                   
                   // if file has .mp3 extension
                   if ($name_array[1] == $file_type) {
                      $play_list .= '<song>';
                      $file = "$dir/$file";
                      $play_list .= '<file>' . $file . '</file>';
                      $mp3 = fopen($file, "r");
                       fseek($mp3, -128, SEEK_END);
                       $tag = fread($mp3, 3);
                       
                       // if id3 tag is found...
                       if ($tag == "TAG") {
                          $play_list .= '<title>' . trim(fread($mp3, 30)) . '</title>';
                          $play_list .= '<artist>' . trim(fread($mp3, 30)) .'</artist>';
                       
                       // if no id3 tag...
                       } else {
                          $play_list .= '<title>unknown title</title>';
                          $play_list .= '<artist>unknown artist</artist>';
                       }
                       
                       // close file
                       fclose($mp3);
                       $play_list .= '</song>';
                   }
                }
             }
             
             // close directory
             closedir($dh);
             $play_list .= '</playlist>';
             
             // echo the xml file
             echo "$play_list";
          }
       }
       
    ?>

Initial URL
http://www.gotoandlearnforum.com/memberlist.php?mode=viewprofile&u=954

Initial Description
I found this script out in space and think it's rad.  Click on the URL to see this authors profile. The easiest way to use this script is to place it in the same directory as your mp3's then make a request to the php.  The php will return an XML with the song Path, Title, and Author which is great for Flash.

Initial Title
PHP MP3 playlist

Initial Tags


Initial Language
PHP