-
Notifications
You must be signed in to change notification settings - Fork 1
/
m3uh.php
66 lines (49 loc) · 1.49 KB
/
m3uh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
* m3uh, a m3u music playlist generator
* Author: Sunny Ripert -- http://sunfox.org
* Licence: GPL -- http://www.gnu.org/copyleft/gpl.html
*/
/* Options */
// Path to the music folder
define('PATH', '../music/');
// You may add a prefix to add to link to your music files
// may be a hard path or a uri, for example:
# define('PREFIX', PATH);
# define('PREFIX', '/home/sunny/music/');
define('PREFIX', 'http://'.$_SERVER['HTTP_HOST'].'/music/');
/* Configuration */
define('EXTENSIONS_RE', '/\.(mp3|ogg|flac)$/');
define('IS_URI', preg_match('/:\/\//', PREFIX));
/* Functions */
// Returns a list of all files in a directory recursively
function files($dir, &$files = array()) {
$handle = opendir($dir);
while ($file = readdir($handle)) {
if ($file == '.' or $file == '..')
continue;
$path = $dir.'/'.$file;
if (is_dir($path))
files($path, $files);
else
$files[] = $path;
}
closedir($handle);
return $files;
}
/* Go! */
$i = 0;
$files = files(PATH);
natcasesort($files);
header('Content-type: audio/x-mpegurl');
echo "#EXTM3U\n";
foreach ($files as $file) {
if (!preg_match(EXTENSIONS_RE, $file))
continue;
++$i;
$name = array_pop(explode('/', $file)); // the name is the last part of the filename
$name = preg_replace(EXTENSIONS_RE, '', $name);
$uri = substr($file, strlen(PATH) + 1); // take off the top path
$uri = IS_URI ? PREFIX . str_replace('%2F', '/', rawurlencode($uri)) : PREFIX . $uri;
echo "#EXTINF:$i,$name\n$uri\n";
}