This recursively finds all files in the given directory, sorts them by last modification date and generates an RSS/Atom feed for your consumption.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

$dir = 'INSERT_FILESYSTEM_DIRECTORY_HERE_WITHOUT_TRAILING_SLASH';
$urlDir = 'http://insert.url/path/here';
$link = 'http://insert.url/thisscript.php';
$limit = 20;
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'stdout');


function date_rfc2822($time) {
  return gmdate('D, d M Y H:i:s O', $time);
}

function rss($data) {
    $body = '<?xml version="1.0" encoding="utf-8"?>';
    $body .= '<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="' . htmlentities($data['baseurl'], ENT_COMPAT, 'utf-8') . '"><channel>';
    $body .= '<title>' . htmlentities($data['title'], ENT_COMPAT, 'utf-8') . '</title>';
    $body .= '<link>' . htmlentities($data['link'], ENT_COMPAT, 'utf-8') . '</link>';
    $body .= '<description>' . htmlentities($data['description'], ENT_COMPAT, 'utf-8') . '</description>';
    $body .= '<language>' . htmlentities($data['lang'], ENT_COMPAT, 'utf-8') . '</language>';
    $body .= '<atom:link href="' . htmlentities($data['linkfeed'], ENT_COMPAT, 'utf-8') . '" rel="self" type="application/rss+xml" />';
    foreach ($data['items'] as $i => $item) {
        $link = htmlentities($item['link'], ENT_COMPAT, 'utf-8');
        $descr = $item['description'];
        $body .= '<item>';
        $body .= '<title>' . htmlentities($item['title'], ENT_COMPAT, 'utf-8') . '</title>';
        $body .= '<link>' . $link . '</link>';
        $body .= '<guid>' . $link . '</guid>';
        foreach($item['enclosures'] as $enclosure) {
            $body .= '<enclosure length="' . $enclosure['length'] . '" type="' . $enclosure['contentType'] . '" url="' . htmlentities($enclosure['url'], ENT_COMPAT, 'utf-8') . '" />';
            $body .= '<media:content fileSize="' . $enclosure['length'] . '" type="' . $enclosure['contentType'] . '" url="' . htmlentities($enclosure['url'], ENT_COMPAT, 'utf-8') . '" />';
        }
        $body .= '<description><![CDATA[' . strip_tags($descr) . ']]></description>';
        $body .= '<content:encoded><![CDATA[' . $descr . ']]></content:encoded>';
        $body .= '<pubDate>' . date_rfc2822($item['pubdate']) . '</pubDate>';
        $body .= '</item>';
    }
    return $body . '</channel></rss>';
}

function find($dir, &$list) {
    if($handle = opendir($dir)) {
        while(false !== ($entry = readdir($handle))) {
            if($entry != '.' && $entry != '..') {
                $path = $dir . '/' . $entry;
                if(is_dir($path)) {
                    find($path, $list);
                } else {
                    $list[$path] = filemtime($path);
                }
            }
        }
        closedir($handle);
    }
}

$found = array();
find($dir, $found);
arsort($found);
$modified = current($found);
if (isset ($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $modified <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
    header('HTTP/1.0 304 Not Modified');
} else {
    header('Content-Type: application/rss+xml; charset=UTF-8');
    header('Last-Modified: ' . date_rfc2822($modified));
    $data = array(
        'baseurl' => $link,
        'title' => 'Latest files from ' . $dir,
        'description' => 'Latest files from ' . $dir,
        'link' => $link,
        'lang' => 'en',
        'linkfeed' => $link,
        'items' => array()
    );
    $i = 0;
    foreach($found as $fullpath => $lastModified) {
        if($i === $limit) {
            break;
        }
        $i++;
        $path = substr($fullpath, strlen($dir));
        $url = $urlDir . str_replace('%2F', '/', rawurlencode($path));
        $data['items'][] = array(
            'link' => $url,
            'description' => $path,
            'title' => $path,
            'pubdate' => $lastModified,
            'enclosures' => array(
                array(
                    'length' => filesize($fullpath),
                    'contentType' => mime_content_type($fullpath),
                    'url' => $url
                )
            )
        );
    }
    echo rss($data);
}