Skip to content

Instantly share code, notes, and snippets.

Forked from vsoch/index.php
Last active December 1, 2022 15:42
Show Gist options
  • Save greg-randall/650ae7566f760da1ff702abfc09ed020 to your computer and use it in GitHub Desktop.
Save greg-randall/650ae7566f760da1ff702abfc09ed020 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, and $feedDesc. Then navigate to the folder on the web to see the xml feed.
header("Content-type: text/xml");
$feed_name = "My Audio Feed";
$feed_description = "Feed for the my audio files in some server folder";
$base_url = strtok('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '?'); //gets the current page's url. strips off the url's parameters
$allowed_extensions = array('mp4','mp3');
<?php echo '<?xml version="1.0"?>'; //we have to use php to output the "<?" ?>
<rss version="2.0" xmlns:atom="">
<title><?php echo $feed_name; ?></title>
<link><?php echo $base_url; ?></link>
<description><?php echo $feed_description; ?></description>
<atom:link href="<?php echo $base_url; ?>" rel="self" type="application/rss+xml" />
$raw_files = scandir ('.'); // get the contents of this directory
foreach ($raw_files as &$raw_file){ //look at each file from the directory
$raw_file_info = pathinfo($raw_file); //get informaition about the file
$extension = strtolower($raw_file_info['extension']); //get the file extension andn make it lowercase
if(in_array ($extension,$allowed_extensions)){ //see if the file extenion on the current file matches what's acceptable
$files[]=$raw_file; //add acceptable files to the array of files
natcasesort($files); //sort the array.
foreach ($files as &$item){ //loop through all items in the array and print an item out for each.
echo " <item>\n";
echo " <title>".substr($item,0,-4)."</title>\n";
echo " <link>$base_url$item</link>\n";
echo " <guid>$base_url$item</guid>\n";
echo " </item>\n";
Copy link

I'm missing something, when I put this in a folder in my server where php is setup all that is outputted is

\n"; echo " \n"; echo " $base_url$item\n"; echo " $base_url$item\n"; echo " \n"; } ?>

I really want to use this so that I can use it for mp3 downloads like a podcast.

Copy link

@hitnrun30 What OS are you running this on? I normally run it on a Linux server, but running it in WAMP on Windows gave odd output like you noted.

Can you try running it on a different system?

Copy link

saywebsolutions commented Aug 15, 2020

I had some files with ampersands in the filename which caused an XML error; a quick str_replace(['&'], '', $item) can fix that for any place that outputs the filename.

I loaded my feed into antenna pod and wasn't able to stream the files via the app; could only click through to listen directly on the site hosting the feed. Added an enclosure tag echo ' <enclosure url="'.$link.'" length="'.$size.'" type="audio/mpeg"/>'; and that seems to have fixed it and I can now stream the files.

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