Skip to content

Instantly share code, notes, and snippets.

@wader
Last active September 3, 2019 17:28
Show Gist options
  • Save wader/4745291 to your computer and use it in GitHub Desktop.
Save wader/4745291 to your computer and use it in GitHub Desktop.
Serve a directory with mp3 files as a podcast feed
<?php
// put this script in web accessable directory with mp3 files and it will
// serve them as a podcast feed. can be used as index.php etc.
// absolute url for current request
function absolute_url() {
return
"http" . ($_SERVER["HTTPS"] != "" ? "s" : "") .
"://{$_SERVER["SERVER_NAME"]}" .
(($_SERVER["HTTPS"] != "" && $_SERVER["SERVER_PORT"] != "443") ||
($_SERVER["HTTPS"] == "" && $_SERVER["SERVER_PORT"] != "80") ?
":" . $_SERVER["SERVER_PORT"] : "") .
$_SERVER["REQUEST_URI"];
}
// remove last part of url path if present
// http://a/b -> http://a/
// http://a/ -> http:/a/
function base_url($url) {
if ($url[strlen($url)-1] == "/") {
return $url;
}
return dirname($url) . "/";
}
// url encode that is path aware
// a b/c -> a%20b/c
function rawurlencode_path($path) {
return implode('/', array_map('rawurlencode', explode('/', $path)));
}
// collect mp3 files in path
function collect_mp3_files($path, $base_url) {
$files = array();
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $name => $_) {
if (pathinfo($name, PATHINFO_EXTENSION) != "mp3") {
continue;
}
$relative_path = substr($name, strlen($path) + strlen("/"));
$files[] = array(
"title" => $relative_path,
"path" => $name,
"url" => $base_url . rawurlencode_path($relative_path),
);
}
return $files;
}
// build rss dom from collection of mp3 files
function podcast_rss_for_files($title, $base_url, $files) {
$rss_dom = new DOMDocument;
$rss_elm = new DOMElement("rss");
$rss_dom->appendChild($rss_elm);
$rss_elm->setAttribute("version", "2.0");
$channel_elm = new DOMElement("channel");
$rss_elm->appendChild($channel_elm);
$title_elm = new DOMElement("title");
$channel_elm->appendChild($title_elm);
$title_elm->appendChild(new DOMText($title));
$channel_elm->appendChild(new DOMElement("desciption"));
$channel_elm->appendChild(new DOMElement("link", $base_url));
foreach($files as $file) {
$item_elm = new DOMElement("item");
$channel_elm->appendChild($item_elm);
$title_elm = new DOMElement("title");
$item_elm->appendChild($title_elm);
$title_elm->appendChild(new DOMText($file["title"]));
$item_elm->appendChild(new DOMElement("desciption"));
$item_elm->appendChild(new DOMElement("link", $file["url"]));
$item_elm->appendChild(new DOMElement("guid", $file["url"]));
$enclosure_elm = new DOMElement("enclosure");
$item_elm->appendChild($enclosure_elm);
$enclosure_elm->setAttribute("url", $file["url"]);
$enclosure_elm->setAttribute("type", "audio/mpeg");
$enclosure_elm->setAttribute("length", filesize($file["path"]));
}
return $rss_dom;
}
header("Content-Type: application/rss+xml");
echo podcast_rss_for_files(
basename(getcwd()),
absolute_url(),
collect_mp3_files(getcwd(), base_url(absolute_url()))
)->saveXML();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment