I have a number of files in a folder, they are all created from a form that names them "time()" so they are all unique, the newest is laways assigned to the bottom of the folder, they are html files. How do I go about reading them in reverse order eg. newest to oldest. they will then be displayed in a page. (1-10) and then the next page will have (10-20)
The folder will be called logs
thanks
andy.
reading from multiple files in reverse order
Moderator: General Moderators
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Get all the files in the directory into an array and then sort the array.
Hopefully that will work.
Code: Select all
$dir = "some/path";
$dp = opendir($dir);
while(false !== ($filename = readdir($dp))){
$files[] = $filename;
}
rsort($files);
print_r($files);-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee
The above code only reads the file names (all of them) into a array, after that you will need to read 10 files from the array and echo them, and put "back | next" links on ur page, something like:
untested...
You will still need to fix few things...i'll let you find what they are
Code: Select all
<?php
//get file names
$dir = "some/path";
$num = 10;
$start = (!isset($_GET['start'])) ? 0 : (int)$_GET['start'];
$dp = opendir($dir);
while(false !== ($filename = readdir($dp)))
{
$files[] = $filename;
}
rsort($files);
fclose($dp);
//read $num files
for($x = $start; $x < ($start + $num); $x++)
{
$file = $dir."/".$files[$x];
echo fread($file);
}
echo '<a href="page.php?start='.($start == 0) ? 0 : $start - $num.'">Back</a>';
echo '<a href="page.php?start='.($start + $num).'">NeXt</a>';
?>You will still need to fix few things...i'll let you find what they are
-
andylyon87
- Forum Contributor
- Posts: 168
- Joined: Sat Jan 31, 2004 5:31 am
- Location: Dundee