$dir = 'directory_of_mp3_files';
$file_type = 'mp3';
$play_list = "<\? xml version='1.0' encoding='UTF-8' standalone='yes'?>";
$play_list .= "<playlist>";
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";
}
}