PHP:
<?php
/**
* Online playlist creator
*
* @version 0.1
* @author Anatoliy Belsky
* @copyright copyleft
*/
/**
* Path to the media files
*/
$dir = '/path/to/files';
/**
* Address where the files are accessible through the web
*/
$url = 'http://host/playlist';
/**
* Which file extensions we want to read
*/
$ext = array('mp3', 'ogg');
/**
* Filelist container
*/
$files = array();
/**
* Read the filelist and build urls
*/
foreach(new DirectoryIterator($dir) as $item) {
if($item->isFile() && in_array(strtolower(substr($item, -3)), $ext)) {
$files[] = "$url/$item";
}
}
/**
* Sort filelist
*/
$sort_method = isset($_GET['sort']) ? $_GET['sort'] : 'rand';
switch($sort_method) {
/**
* Alphabetical order
*/
case 'alpha':
asort($files);
break;
/**
* Reversed alphabetical order
*/
case 'ralpha':
arsort($files);
break;
/**
* Natural case insensitive order
*/
case 'natcase':
natcasesort($files);
break;
case 'rand':
default:
srand((float)microtime() * 1000000);
shuffle($files);
break;
}
/**
* Manage the output format
*/
$format = isset($_GET['format']) ? $_GET['format'] : 'm3u';
switch($format) {
case 'txt':
$mime_type = 'text/plain';
break;
case 'm3u':
default:
$mime_type = 'audio/x-mpequrl';
break;
}
/**
* Output headers and the playlist contents
*/
header('Status: 200 OK');
header("Content-Type: $mime_type");
header('Content-Disposition: inline; filename="playlist.m3u"');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
print join("\n", $files);
Now you can access your playlist just calling the url like http://host/playlist.php?sort=rand and hear all the music directly from the server.
A bit tricky part here is to set a correct mime type. It's because quite all players under windows (for example winamp) require a playlist file with the m3u extension or alike. But even though they do not understand headers we send. Opening the url inside browser does the job.
At the same time calling an url like this http://host/playlist.php?sort=rand&format=txt within any linux player does work. For example Kaffeine accepts just a text file with any filelist and is able to play it. So enjoy Unix, if you can )).
Possibilities not implemented here just like a password policy, other playlist and media file formats, outputting files outside web root etc. you could do yourself, if you want to.
And yeah, an important thing - just be sure you have enough traffic to do such things with your server.
So, happy hearing
