Im当前正在输出一个目录中保存的所有文本文件:
$directory = "polls/";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if ($type == 'file') {
$contents = file_get_contents($filename);
list($tag, $name, $description, $text1, $text2, $text3, $date) = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
echo "<tr><td>$tag</td></tr>\n";
echo "<tr><td>$name</td></tr>\n";
echo "<tr><td>$description</td></tr>\n";
echo "<tr><td>$text1</td></tr>\n";
echo "<tr><td>$text2</td></tr>\n";
echo "<tr><td>$text3</td></tr>\n";
echo "<tr><td>$date</td></tr>\n";
echo '</table>';
}
}
closedir($dir);我想扩展这一点,以便在输出文件之前对文件进行排序,因此应该使用unixtime格式的文件名。然后,我只想在排序后输出前5个。这应该会给我最新的5分。
发布于 2010-01-02 21:25:05
相反,您可以使用glob (它按升序对文件进行排序),只提取该文件路径数组中的一部分。例如:
$files = glob('polls/[0-9]*.txt');
$reverse = array_reverse($files);
$latest = array_slice($reverse, 0, 10);
foreach ($latest as $file) {
// ...
}发布于 2010-01-02 20:18:41
如果数据尚未排序,则必须构建一个文件数组,使用sort对它们进行排序,然后使用array_slice提取前五项。
https://stackoverflow.com/questions/1991442
复制相似问题