Skip to content

Instantly share code, notes, and snippets.

@vsoch
Last active June 2, 2024 14:28
Show Gist options
  • Save vsoch/4898025919365bf23b6f to your computer and use it in GitHub Desktop.
Save vsoch/4898025919365bf23b6f to your computer and use it in GitHub Desktop.
Generate RSS feed for files in a directory folder. Put this file in a folder with files, modify the $allowed_ext variable to customize your extensions, and $feedName, $feedDesc, $feedURL, and $feedBaseURL. Then navigate to the folder on the web to see the xml feed. Done!
<?php
header('Content-type: text/xml');
/*
Runs from a directory containing files to provide an
RSS 2.0 feed that contains the list and modification times for all the
files.
*/
$feedName = "My Audio Feed";
$feedDesc = "Feed for the my audio files in some server folder";
$feedURL = "http://www.mysite.com/audio";
$feedBaseURL = "http://www.mysite.com/audio/"; // must end in trailing forward slash (/).
$allowed_ext = ".mp4,.MP4,.mp3,.MP3";
?><<?= '?'; ?>xml version="1.0"<?= '?'; ?>>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><?=$feedName?></title>
<link><?=$feedURL?></link>
<description><?=$feedDesc?></description>
<atom:link href="http://gogglesoptional.com/bloopers" rel="self" type="application/rss+xml" />
<?php
$files = array();
$dir=opendir("./");
while(($file = readdir($dir)) !== false)
{
$path_info = pathinfo($file);
$ext = strtoupper($path_info['extension']);
if($file !== '.' && $file !== '..' && !is_dir($file) && strpos($allowed_ext, $ext)>0)
{
$files[]['name'] = $file;
$files[]['timestamp'] = filectime($file);
}
}
closedir($dir);
// natcasesort($files); - we will use dates and times to sort the list.
for($i=0; $i<count($files); $i++) {
if($files[$i] != "index.php") {
if (!empty($files[$i]['name'])) {
echo " <item>\n";
echo " <title>". $files[$i]['name'] ."</title>\n";
echo " <link>". $feedBaseURL . $files[$i]['name'] . "</link>\n";
echo " <guid>". $feedBaseURL . $files[$i]['name'] . "</guid>\n";
echo " <pubDate>". date(DATE_RSS, $files[$i]['timestamp']) ."</pubDate>\n";
// echo " <pubDate>". date("D M j G:i:s T Y", $files[$i]['timestamp']) ."</pubDate>\n";
// echo " <pubDate>" . $files[$i]['timestamp'] ."</pubDate>\n";
echo " </item>\n";
}
}
}
?>
</channel>
</rss>
@lumibliss
Copy link

Hmm, seems to be quite a few bugs with this. I also got 'this feed contains no entries'. Key problem is that the feed creates <link> entries for each item, but no <enclosure> tags which also requires size and filetype.

Here's what I ended up with. I also changed the array code which was really messed up, and not actually adding unique entries for the dates of each file, and fixed the pathinfo code.

limitation though: No intelligent code for filetype. It just returns the same string every time.

<?php
$items = array();
$sub = "";
$dir=opendir("./".$sub);

while(($file = readdir($dir)) !== false)
{
        $ext = strtoupper(pathinfo($sub.$file, PATHINFO_EXTENSION));
        if($file !== '.' && $file !== '..' && !is_dir($file) && $ext !== '' && strpos($allowed_ext, $ext)>0)
        {
                $item['name'] = $sub.$file;
                $item['timestamp'] = filectime($sub.$file);
                $item['size'] = filesize($sub.$file);
                $items[] = $item;
        }
}
closedir($dir);
// natcasesort($files); - we will use dates and times to sort the list.
foreach($items as $item) {
        if($item['name'] != "index.php") {
          if (!empty($item['name'])) {
                echo "  <item>\n";
                echo "          <title>". $item['name'] ."</title>\n";
                echo "          <enclosure url='". $feedBaseURL . $item['name']. "' length='" . $item['size'] . "' type='video/mp4' />\n";
                echo "          <guid>". $feedBaseURL . $item['name'] . "</guid>\n";
                echo "          <pubDate>". date(DATE_RFC822, $item['timestamp']) ."</pubDate>\n";
                //echo "                <pubDate>" . $files[$i]['timestamp'] ."</pubDate>\n";
                echo "    </item>\n";
          }
        }
}
?>

`

@davet2001

Hey man,

Is there a way to contact you?

I kinda wanna request your help with the github code that you are familiar with:
https://gist.github.com/vsoch/4898025919365bf23b6f

I can't seem to make it work for my website..

I would appreciate it if you can leave an email so I can ask you about it.

Thanks in advance!

@davet2001
Copy link

@lumibliss Yeah that was a long time ago, I can’t say I’m super familiar with it now, but I’ll try to help where I can!
Are you able to read debug output from the PHP server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment