Create an RSS Feed of Files in a Directory
I record some radio broadcasts for offline/on-demand listening. Coupled with an RSS feed, I can get these downloaded to my podcast player of choice. Below is an example of such a feed that I put together last night.
<?php
$files = glob("/path/to/directory/*");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
$files = array_keys($files);
echo '<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Title of Your Feed</title>
<description>Description of Your Feed</description>
<link>A homepage for your feed</link>
<language>en-us</language>
<itunes:image href="http://path/to/feed/icon.jpg"/>
';
foreach ( $files as $file) {
// the filenames that I'm working with include the date in them
// with a little slicing and dicing of the names, I can get what I need for the date variable
// there are likely better ways of doing this, but this seemed the mostly reliable route to take in my case
$date = preg_replace('/regex.of.junk/', '', $file);
$date = preg_replace('/regex.of.more.junk/', '', $date);
$date = date('D, j M Y H:i:s', strtotime($date));
// remove the directory structure from the filename
// again, there are likely better ways of doing this, but I was on a preg_replace roll
$file = preg_replace('/.*Filename/', 'Filename', $file);
echo "
<item>
<title>Title " . $date . "</title>
<link>Link</link>
<guid>" . $file . "</guid>
<pubDate>" . $date . "</pubDate>
<enclosure url='http://download/link/path/" . $file . "' type='audio/mpeg'/>
</item>
";
// let's stop after creating an RSS item for 10 files
if (++$i == 9) break;
}
echo "
</channel>
</rss>
";
?>