Jump to content


Photo

Reading a txt file from bottom to top


  • Please log in to reply
3 replies to this topic

#1 battsb

battsb
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 May 2006 - 09:23 PM

Hi guys, I'm a semi-noob....rather a 'professional amateur', and I need alittle assistance with something. I'm surprised php doesn't have any built-in functions for reading a file LINE by LINE, from the bottom to the top, but I did see a few ways of doing it, using fseek()...but nothing that reads a file line by line....All the examples on the php.net site show it character by character. Any suggestions?

I'd think it'd be a pretty common needed task (to read from bottom to top), since files can be appended to at the bottom, meaning all the latest information starts at the bottom. Does anyone have any pointers? (pun not intended) :)



#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 May 2006 - 09:26 PM

file read to array - [a href=\"http://ca.php.net/manual/en/function.file.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.file.php[/a]

then use count() for size of array to get the end element, etc.

reverse array might be useful to you as well - [a href=\"http://ca.php.net/manual/en/function.array-reverse.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.array-reverse.php[/a]
Legend has it that reading the manual never killed anyone.
My site

#3 battsb

battsb
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 May 2006 - 12:25 AM

[!--quoteo(post=374436:date=May 16 2006, 05:26 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 16 2006, 05:26 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
file read to array - [a href=\"http://ca.php.net/manual/en/function.file.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.file.php[/a]

then use count() for size of array to get the end element, etc.

reverse array might be useful to you as well - [a href=\"http://ca.php.net/manual/en/function.array-reverse.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.array-reverse.php[/a]
[/quote]

I can't use that because the file can be up to 100Meg, which is why I need to read from the bottom up, because I will only need the bottom 1,000 lines (based on date) or so.

Eg of chronological log file contents
V,<date&time>,0,150
V,<dateTime>,0,14d
D,<dateTime>,0,16e

I need to get to the bottom 1,000 lines or so.....in <1 sec. Possible?


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 May 2006 - 12:56 AM

maybe - [a href=\"http://ca3.php.net/manual/en/function.fseek.php\" target=\"_blank\"]http://ca3.php.net/manual/en/function.fseek.php[/a] and take a look at the scriptlet posted by phil at NOSPAM dot blisswebhosting dot com
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users