|
Posted by jukka on 09/28/21 11:42
So I coded this flat file database, and one module is a script that
displays five latest entries (script below). The files in dir/ are
stored as 0001.html, 0002.html, etc via another script. Each html file
has a html commented header that looks something like this:
<!--
title=Hello, this is the title
date=2006-01-23
-->
So this here script gets the title from the header and displays it. Now
I'm wondering that if I have several files in the directory, would it be
too heavy a script to have to read through all of them (since the
fread() can make the script pretty heavy anyway)? Is there a way to get
just the last five filenames in the directory _without_ reading the
whole directory? This way I could include the last five files in a
blog-like way, without having to read the whole dir/ with glob().
// This script generates links to the latest five entries in dir/.
<html>
<head>
<title>flatfile-database</title>
</head>
<body>
<?php
$dir = glob("dir/*.html");
if (!$dir) {
echo "Directory is empty!<br>\n";
}
else {
$files = array_reverse($dir);
for ($j=0; $j<=4; $j++) {
$handle = fopen($files[$j],"r");
$file_text = fread($handle, filesize($files[$j]));
$temp_title = strstr($file_text, "title=");
$line_end = strpos($temp_title, "\n");
$title = substr($temp_title, 6, $line_end-6);
fclose($handle);
echo "<a href=\"".$files[$j]."\">".$title."</a>\n<br>\n";
}
}
?>
</body>
</html>
Navigation:
[Reply to this message]
|