While avoiding working on (franken)log I actually came across a slightly major issue and wanted to resolve it right away.
What was happening was that the rss.php and dailyfeed.php scripts weren’t running properly and erroring out before they could output their respective .xml files. I knew what the error was because of the error_log:
PHP Warning: file_get_contents(/home/xxx/ log.kvl.me/posts/ 1969/12/ test.md.md): failed to open stream: No such file or directory in /home/xxx/log.kvl.me/dailyfeed.php on line 53
However, I didn’t really know why it was happening or how to fix it.
Three or four versions of this error were popping up, which was weird because the files they were referencing didn’t exist. Some type of ‘ghost files.’ This was happening both with the 1969-12 folder, but also for more recent folders where I previously deleted a dated .md post file manually (I assume).
A solution (the one I implemented) was to check to make sure that the files being referenced actually exist— I find it funny that this actually needs to be done at all, and to exclude these ‘ghost files’ from populating in the feed. It took a couple of tries to get it working just right— I had to make sure to check that the files existed before sorting the files and selecting the most recent ones, but the code eventually got there.
It seems to be working for both rss.php and dailyfeed.php, and I no longer see random 1969 entries that sometimes would appear when rss.php would run correctly. I’ll have to keep a close eye on this for any funny business over the next few weeks.
The Implemented Solution
$postfiles = glob($target_dir.'/*/*/*/*.md');
// check if there are any matching files
if (empty($postfiles)) {
echo "No matching files found.\n";
exit();
}
$filedates = [];
foreach($postfiles as $postfile) {
if (substr(explode('/',$postfile)[7],0,1) != 'c') {
$filedate = substr(explode('/',$postfile)[7],0,10);
$year = date('Y', strtotime($filedate));
$month = date('m', strtotime($filedate));
$filename = $target_dir.'/posts/'.$year.'/'.$month.'/'.$filedate.'.md';
if (file_exists($filename)) {
$filedates[] = $filedate;
}
}
}
// get files and use only last 5
rsort($filedates);
array_splice($filedates,5);
//get posts from each file
foreach($filedates as $file) {
$fullcontent = '';
$year = date('Y', strtotime($file));
$month = date('m', strtotime($file));
$filename = $target_dir.'/posts/'.$year.'/'.$month.'/'.$file.'.md';
$posts = file_get_contents($filename);
// process the contents of the file here
}