Generate a dynamic playlist of mp3 files in a folder with PHP

Below is a simple php script to generate a playlist of the mp3 files placed in a folder. I have tested it to work with XMMS media player on FC4 and should work with others too. Substitute the path and the url of the music folder.

// music_playlist.php

$folder_path = './music_folder';
$folder_url = 'http://domain.tld/music_folder/';

if ($handle = opendir($folder_path)) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           $song_list .= $folder_url.$file."\n";
else {
  echo "Music folder does not exist!";

// send header of audio/x-mpegurl content type
header('Content-type: audio/x-mpegurl');
if (preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
       header("Content-Disposition: filename=\"my_playlist.m3u\"");
else {
       header("Content-Disposition: inline; filename=\"my_playlist.m3u\"");  

echo "$song_list";


Related Reading: Listen to mp3 songs using XMMS in Redhat/Fedora