$time)
{
// Create item/pubDate according to RFC822
$itemPubDate = date("r", $time);
// Also use first item/pubDate as channel/pubDate
if($channelPubDate==null) $channelPubDate = $itemPubDate;
// Absolute path to ftp file
$fileUri = ereg_replace(" ", "%20", $linkPrefix . $filePath);
// Create item
$item = '- '
.'' . $fileUri . ''
.''. $fileUri . ''
.'' . $itemPubDate . ''
.'
';
// Add to item array
array_push($items, $item);
// If max items for feed reached, stop
if(count($items)==$itemCount) break;
}
// Build RSS feed
$rss = ''
. ''
. 'FTP Monitor: ' . $host . ''
. '' . $linkPrefix . ''
. 'The ' . $itemCount.' latest changes on '
. $host . $path . ' (out of ' . count($fileList)
. ' files)'
. '' . $channelPubDate . '' . "\n"
. join("\n", $items) . "\n"
. ''
. '';
// Set header for XML mime type
header("Content-type: text/xml; charset=UTF-8");
// Display RSS feed
echo($rss);
}
/**
* Gets files and their timestamps of an FTP
* server directory and its children.
*
* @return an associative array where keys are
* filenames and values are timestamps
*/
function exploreFtpServer($host, $user, $pass, $path="/")
{
// Connect
$cid = ftp_connect($host) or die("Couldn't connect to server");
// Login
if (ftp_login($cid, $user, $pass))
{
// Passive mode
ftp_pasv($cid, true);
//echo("Scanning " . $host . " ...
");
// Recurse directory structure
$fileList = scanDirectory($cid, $path);
// Disconnect
ftp_close($cid);
}
else
{
// Disconnect
ftp_close($cid);
die("Couldn't authenticate.");
}
// Sort by timestamp, newest (largest number) first
arsort($fileList);
// Return the result
return $fileList;
}
/**
* Scan a directory for files, collecting timestamps
* and recursing into subdirectories.
*
* @return an associative array where keys are
* filenames and values are timestamps
*/
function scanDirectory($cid, $dir)
{
// Use static value to collect results
static $fileList=array();
// Get a listing of directory contents
// Note: wu-ftpd servers will not include directories
$contents = ftp_nlist($cid, $dir);
// Iterate through the directory contents
foreach ($contents as $item)
{
// Is the item a file?
if (ftp_size($cid, $item)>=0)
{
// Prepend slash if not present
if($item[0]!="/") $item = "/" . $item;
// Add file and modify timestamp to results
$fileList[$item] = ftp_mdtm($cid, $item);
}
else
// Item is a directory
{
// Exclude self/parent aliases
if($item!="." && $item!=".." && $item!="/")
{
// Server uses full path names
if($item==strstr($item, $dir))
{
scanDirectory($cid, $item);
}
else
{
// Server uses relative path names
if($dir=="/")
{
scanDirectory($cid, $dir . $item);
}
else
{
scanDirectory($cid, $dir . "/" . $item);
}
}
}
}
}
// Return the results
return $fileList;
}
?>