How to read newest files only, in a text file.

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
Fluxcore
Forum Newbie
Posts: 3
Joined: Sun Jul 28, 2002 11:56 am
Location: Mi.
Contact:

How to read newest files only, in a text file.

Post by Fluxcore »

I have a text file that a form writes to. Then the output is printed on the screen, but the output gets to be to long on the screen. Is there a way to read the 10 newest entries only? Thanks.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

ok say "file.txt" contained

Code: Select all

foo
bar
barz
foobar
foobarbarz
and i did this script:

Code: Select all

$file = file("file.txt");
$file = array_reverse($file);
$file = array_slice($file, 0, 3);
foreach($file as $data){
 echo $data;
 echo "<br /> \n";
&#125;
then it should print out:

Code: Select all

foobarbarz
foobar
barz
got the idea? look into http://www.php.net/file and http://www.php.net/array_reverse and http://www.php.net/array_slice
Fluxcore
Forum Newbie
Posts: 3
Joined: Sun Jul 28, 2002 11:56 am
Location: Mi.
Contact:

Post by Fluxcore »

Thank you, worked wonderful, thanks for the fast help.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Glad to know it helped. I wasn't sure if you had information on single lines or not.
Post Reply