reading from multiple files in reverse order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

reading from multiple files in reverse order

Post by andylyon87 »

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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Get all the files in the directory into an array and then sort the array.

Code: Select all

$dir = "some/path";
$dp = opendir($dir);
while(false !== ($filename = readdir($dp))){
   $files[] = $filename;
}

rsort($files);

print_r($files);
Hopefully that will work.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Opps, you want closedir($dp); in there at the end :P
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

kettle_drum wrote:Opps, you want closedir($dp); in there at the end :P
(psst, you can edit your own posts after creation) :wink:
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

Does this allow me to read them say like in a bulletin board, i want the files to be read from in reverse order is there a way of getting tit to do this.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

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:

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>';
?>
untested...

You will still need to fix few things...i'll let you find what they are :twisted:
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

a bit harsh but thanks all the same :D
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

we can only show you the door, you gota open it your self :lol:
Post Reply