-
-
Save tnorthcutt/129e5f054b1b62510bc1aea13051a4a7 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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 = "Name"; | |
$feedDesc = "Description"; | |
$feedURL = "https://example.com/"; | |
$feedBaseURL = "https://example.com/"; // must end in trailing forward slash (/). | |
$allowed_ext = ".mp3"; | |
$files = array(); | |
$dir=opendir("./"); | |
$i = 0; | |
while(($file = readdir($dir)) !== false) | |
{ | |
$path_info = pathinfo($file); | |
$ext = strtolower($path_info['extension']); | |
if($file !== '.' && $file !== '..' && !is_dir($file) && strpos($allowed_ext, $ext)>0) | |
{ | |
$files[$i]['name'] = $file; | |
$files[$i]['timestamp'] = filemtime($file); | |
$files[$i]['length'] = filesize($file); | |
$i++; | |
} | |
} | |
closedir($dir); | |
?><<?= '?'; ?>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 | |
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 ' <enclosure url="' . $feedBaseURL . $files[$i]["name"] . '" length="' . $files[$i]["length"] . '" type="audio/mpeg"/>'; | |
echo "\n"; | |
echo " <guid>". $feedBaseURL . $files[$i]['name'] . "</guid>\n"; | |
echo " <pubDate>". date(DATE_RSS, $files[$i]['timestamp']) ."</pubDate>\n"; | |
echo " </item>\n"; | |
} | |
} | |
} | |
?> | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment