Posted By

karlhorky on 11/08/10


Tagged

list rss feed folder files directory modified listing latest newest directorylist directorylisting


Versions (?)

RSS Feed Folder v1.1


 / Published in: PHP
 

URL: http://www.rssFeedFolder.com/

This script is a modification of the RSS Feed Folder script available at http://www.rssFeedFolder.com/ . The original script created an RSS feed based on the latest changed files by html title and meta description tags. My modification removes the html components of the original script, instead aiming to generate an RSS feed with the latest files (recursive) in the specified directory.

The updated script achieves this by looping recursively through all directories that it finds within the specified path and listing out the file names and modification times (filemtime) as it goes. Once it has an array of all filenames with their modification times, it reverse sorts this list and does an array_slice on the first $rssMaxItems to be displayed. Finally, the script echos all the items in RSS format.

  1. <?php
  2. ###########################################################################
  3. ###
  4. ### RSS FEED FOLDER
  5. ### http://www.rssFeedFolder.com
  6. ###
  7. ### v1.1
  8. ### Nov 7th 2010
  9. ### Author: Karl Horky
  10. ### Purpose: Display list of files in given directory, starting from latest
  11. ###
  12. ### v1.0
  13. ### June 3rd 2009
  14. ###
  15. ###########################################################################
  16.  
  17.  
  18. ###########################################################################
  19. ###
  20. ### Edit these next few lines to customize your feed
  21. ###
  22. ###########################################################################
  23. $rssTitle = "RSS Feed Title";
  24. $rssDescription = "RSS feed created using tools from http://www.rssFeedFolder.com";
  25. $rssMaxItems = (isset($_GET['items'])) ? $_GET['items'] : 50;
  26. $rssLink = "http://www.rssFeedFolder.com/";
  27. $rssLanguage = "en";
  28. $rssCopyright = "rssFeedFolder.com";
  29. $rssTtl = "1000";
  30. //$rssLogo = "http://www.rssFeedFolder.com/images/rssFeedFolderLogo.gif";
  31. // Example directory to list items from
  32. $rssFolder = "Music";
  33. // Make $strDocRoot point at the parent of your example directory ("Music" above)
  34. $strDocRoot = str_replace('htdocs', 'media', $_SERVER['DOCUMENT_ROOT']);
  35.  
  36.  
  37. ###########################################################################
  38. ### The name of the folder to build the feed from is an optional parameter
  39. ### e.g. http://www.yourdomain.com/cgi-bin/rss.cgi?feed=news
  40. ### http://www.yourdomain.com/cgi-bin/rss.cgi?feed=overstock
  41. ### The $rssFolder setting is used if you just call
  42. ### http://www.yourdomain.com/cgi-bin/rss.cgi
  43. ###########################################################################
  44. $feed = $_GET['feed'];
  45. if ( $feed == "" ) {
  46. $feed = $rssFolder;
  47. }
  48.  
  49.  
  50. ###########################################################################
  51. ### Determine website config details to locate and create the feed links.
  52. ###########################################################################
  53. $strTimeZone = "GMT";
  54. $strHost = $_SERVER['SERVER_NAME'];
  55. $strScript = $_SERVER['SCRIPT_NAME'];
  56. $rawDate = exec("date");
  57. $rawDate = rtrim($rawDate);
  58. $rssDate = rfcDate($rawDate);
  59.  
  60.  
  61. ###########################################################################
  62. ### Start the Feed
  63. ###########################################################################
  64. header('Content-type: application/rss+xml');
  65. echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">
  66. ";
  67. echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
  68. ";
  69. echo " <channel>
  70. ";
  71.  
  72.  
  73. ###########################################################################
  74. ### Feed owner details
  75. ###########################################################################
  76. echo " <title>$rssTitle</title>
  77. ";
  78. echo " <description>$rssDescription</description>
  79. ";
  80. echo " <link>$rssLink</link>
  81. ";
  82. echo " <language>$rssLanguage</language>
  83. ";
  84. echo " <copyright>$rssCopyright</copyright>
  85. ";
  86. echo " <pubDate>$rssDate</pubDate>
  87. ";
  88. echo " <lastBuildDate>$rssDate</lastBuildDate>
  89. ";
  90. echo " <generator>rssFeedFolder.com</generator>
  91. ";
  92. echo " <ttl>$rssTtl</ttl>
  93. ";
  94. echo " <atom:link href=\"http://${strHost}${strScript}\" rel=\"self\" type=\"application/rss+xml\" />
  95. ";
  96.  
  97.  
  98.  
  99. ###########################################################################
  100. ### The items in the feed
  101. ###########################################################################
  102. $lArticles = array();
  103. $cArticles = 0;
  104.  
  105. // Loop recursively through all directories in specified path and list all files
  106. $files = array();
  107. function getFiles($strDocRoot, $path, &$files) {
  108. foreach (glob("$path/*") as $node) {
  109. if (!is_dir($node)) {
  110. $files[str_replace($strDocRoot, '', $node)] = filemtime($node);
  111. } else {
  112. getFiles($strDocRoot, $node, $files);
  113. }
  114. }
  115. }
  116. getFiles($strDocRoot, "$strDocRoot$feed", $files);
  117. arsort($files);
  118. $newestFiles = array_slice($files, 0, $rssMaxItems);
  119.  
  120.  
  121. //foreach ( glob("$strDocRoot/$feed/*") as $strFile )
  122. foreach ($newestFiles as $strFile => $filemtimeFile)
  123. {
  124. $cArticles++;
  125.  
  126. ###################################################################
  127. ### Published Date is the modification date of the feed item
  128. ###################################################################
  129. $strPubDate = date( DATE_RSS, $filemtimeFile );
  130.  
  131.  
  132. ###################################################################
  133. ### Print the item info
  134. ###################################################################
  135. $strArticle = "";
  136. $strArticle .= " <item>
  137. ";
  138. $strArticle .= " <title><![CDATA[$strFile]]></title>
  139. ";
  140. $strArticle .= " <pubDate>$strPubDate</pubDate>
  141. ";
  142. $strArticle .= " </item>
  143. ";
  144. echo $strArticle;
  145. }
  146.  
  147. echo " </channel>
  148. ";
  149. echo "</rss>
  150. ";
  151.  
  152.  
  153.  
  154. ###########################################################################
  155. ### Translate UNIX date format into the format required by RSS
  156. ### which is: Tue, 04 Dec 2007 15:22:43 CST
  157. ###########################################################################
  158. function rfcDate( $date )
  159. {
  160. $ret = "";
  161.  
  162. ###################################################################
  163. ### Tue Dec 4 15:22:43 CST 2007 => Tue, 04 Dec 2007 15:22:43 CST
  164. ###################################################################
  165. preg_match( '/^([A-Za-z]+) ([A-Za-z]+) +(\d+) ([0-9:]+) ([A-Z]+) (\d+)$/', $date, $matches );
  166. if ( $matches )
  167. {
  168. $ret = sprintf("%s, %2.2d %s %d %s %s", $matches[0], $matches[2], $matches[1], $matches[5], $matches[3], $matches[4]);
  169. $strTimeZone = $matches[4];
  170. }
  171. ###################################################################
  172. ### Tue Dec 4 15:22:43 2007 => Tue, 04 Dec 2007 15:22:43 CST
  173. ###################################################################
  174. else
  175. {
  176. preg_match( '/^([A-Za-z]+) ([A-Za-z]+) +(\d+) ([0-9:]+) +(\d+)$/', $date, $matches );
  177. if ( $matches )
  178. {
  179. $ret = sprintf("%s, %2.2d %s %d %s %s", $matches[0], $matches[2], $matches[1], $matches[4], $matches[3], $strTimeZone);
  180. }
  181. }
  182.  
  183. return $ret;
  184. }
  185. ?>

Report this snippet  

You need to login to post a comment.