View Full Version : Read Last 100 Entries from a Data File

08-28-2011, 03:14 AM
Could somebody please show me how to always read only the last 100 entries from a text data file like this?



08-28-2011, 04:09 AM
There might be other ways but this works

$file_handle = fopen("http://bybyron.uni.cc/server_data/server_data_July_2011.dat", "r");
while (!feof($file_handle) ) {
$parts[$ax] = fgets($file_handle);
if($ax>100) {
for ( $counter = $ax-100; $counter <= $ax; $counter += 1) {
echo $parts[$counter];
} else {
for ( $counter = 1; $counter <= $ax; $counter += 1) {
echo $parts[$counter];

08-28-2011, 04:37 AM
This will display the last $numRecords records of the file.


$lines = file('test.dat');
$numRecords = 5; //number of records from eof to read
if (count($lines) >= $numRecords) {
for ($i = count($lines) - $numRecords; $i < count($lines); $i++) {
echo $lines[$i] . '<br />';
} else {
for ($i = 0; $i < count($lines); $i++) {
echo $lines[$i] . '<br />';

08-28-2011, 05:07 AM
Thanks guys! Both solutions work great!

08-28-2011, 05:21 AM
The main difference between the 2 solutions is that the file() function does all the opening, reading and closing of the data file.

08-28-2011, 06:04 AM
The main difference between the 2 solutions is that the file() function does all the opening, reading and closing of the data file.

Ok, thank you! :)

08-28-2011, 04:20 PM
I agree, Webdev1958 showed me something new to save me time.

08-28-2011, 09:36 PM
Unfortunately as your filesize increases, the probability of exhausting your memory will also increase. There is no real solution to this, but IMO to combat it you would need to use an fopen technique with a line skip and count back in a 100 entry buffer. What a pain.
If you can actually create this data file though, you could create a random access file instead and use seek. Since you only have the date and a server load, you can do that with a single integer and a single float and parse out the data to display as you see fit. The pro to this is that you now know each record is 64 bits in size, and you can use fseek in combination with 100x the 8bytes, and count backwards.

So, if I converted these into an strtotime and added a float for the load, I'd end up with a dat file like this (no header, so you may want to consider adding that in).

N @;?
N @|w
N33@,NffV@_.NRn@[<NQ@fNR@tN@̞N@۬NQ@ N@N\@;NH@LNzT@{9NHz@cN
N33C@%Nfff@+Nk@LN{@`NG?@N@,N\?@<Nף@KN <A`Nq=&AjNH*A{Nz,A? Nq=AN @%Np@3N?@AN@ONz@]N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NG1@lr1Np-@{1NR.@1Nff?1NR@1N{@1N @1NH@1NX@1NHz@
3@*2N9@2N@̧2Nq=@ݵ2NG?@2NzD@2NE@2NX@2N A,2Nz@<
3N{.@L3NG@d&3Nz4@p43Nף?|B3N33@P3NQ@l3Nff@z3N@̈3N `@ޖ3N=
׃@ 3N(|@3NzT@-3N{^@<3NzD@L3N*@]4NQ8@l4N=
@i4NG?w4N?4N?@4N{@ 4NY@74N\?B@,4N)\_@<4NGa@L4NRN@b4Np@l4N
c@{5Np@5NH@ 5NG@.5N@<5N)\_@J5Nף@X5N{^@f5N;@t5N@ 5N{N@5NRN@,5N{~@[5NH@]5Np?@o5N{~@|5N=

And read with:


$sPath = './newseek.dat';

$iToRecord = 100;
$iRecordSize = 8;

print '<ol>';

if ($fh = fopen($sPath, 'r'))
fseek($fh, -($iToRecord * $iRecordSize), SEEK_END);
while ($sRecord = fread($fh, $iRecordSize))
$aUnpacked = unpack('idate/fload', $sRecord);
$iTime = $aUnpacked['date'];
$fLoad = $aUnpacked['load'];
printf('<li class="%s">%s | Server Load:*%0.2f</li>' . PHP_EOL, date('D', $iTime), date('D jS M, h:i:s a', $iTime), $fLoad);
print '</ol>';

Results in:

<ol><li class="Wed">Wed 27th Jul, 08:30:04 am | Server Load:*4.96</li>
<li class="Wed">Wed 27th Jul, 09:30:04 am | Server Load:*5.34</li>
<li class="Wed">Wed 27th Jul, 10:30:04 am | Server Load:*5.18</li>
<li class="Wed">Wed 27th Jul, 11:30:06 am | Server Load:*5.69</li>
<li class="Wed">Wed 27th Jul, 12:30:04 pm | Server Load:*4.02</li>
<li class="Wed">Wed 27th Jul, 01:30:05 pm | Server Load:*3.74</li>

<li class="Wed">Wed 27th Jul, 02:30:04 pm | Server Load:*4.31</li>
<li class="Wed">Wed 27th Jul, 03:30:04 pm | Server Load:*4.53</li>
<li class="Wed">Wed 27th Jul, 04:30:04 pm | Server Load:*6.93</li>
<li class="Wed">Wed 27th Jul, 05:30:04 pm | Server Load:*3.71</li>
<li class="Wed">Wed 27th Jul, 06:30:04 pm | Server Load:*7.43</li>
<li class="Wed">Wed 27th Jul, 07:30:04 pm | Server Load:*12.41</li>

<li class="Wed">Wed 27th Jul, 08:30:05 pm | Server Load:*8.31</li>
<li class="Wed">Wed 27th Jul, 09:30:04 pm | Server Load:*9.87</li>
<li class="Wed">Wed 27th Jul, 10:30:04 pm | Server Load:*7.69</li>
<li class="Wed">Wed 27th Jul, 11:30:04 pm | Server Load:*11.05</li>
<li class="Thu">Thu 28th Jul, 12:30:04 am | Server Load:*25.52</li>
<li class="Thu">Thu 28th Jul, 08:30:04 am | Server Load:*2.07</li>

<li class="Thu">Thu 28th Jul, 09:30:05 am | Server Load:*3.78</li>
<li class="Thu">Thu 28th Jul, 10:30:04 am | Server Load:*4.30</li>
<li class="Thu">Thu 28th Jul, 11:30:04 am | Server Load:*3.97</li>
<li class="Thu">Thu 28th Jul, 12:30:03 pm | Server Load:*2.95</li>
<li class="Thu">Thu 28th Jul, 01:30:04 pm | Server Load:*2.77</li>
<li class="Thu">Thu 28th Jul, 02:30:04 pm | Server Load:*2.71</li>

<li class="Thu">Thu 28th Jul, 03:30:03 pm | Server Load:*2.73</li>
<li class="Thu">Thu 28th Jul, 04:30:04 pm | Server Load:*1.80</li>
<li class="Thu">Thu 28th Jul, 05:30:04 pm | Server Load:*2.23</li>
<li class="Thu">Thu 28th Jul, 06:30:04 pm | Server Load:*4.94</li>
<li class="Thu">Thu 28th Jul, 07:30:05 pm | Server Load:*5.75</li>
<li class="Thu">Thu 28th Jul, 09:30:04 pm | Server Load:*7.84</li>

<li class="Thu">Thu 28th Jul, 10:30:05 pm | Server Load:*3.39</li>
<li class="Thu">Thu 28th Jul, 11:30:04 pm | Server Load:*3.92</li>
<li class="Fri">Fri 29th Jul, 12:30:05 am | Server Load:*4.59</li>
<li class="Fri">Fri 29th Jul, 01:30:05 am | Server Load:*2.38</li>
<li class="Fri">Fri 29th Jul, 02:30:04 am | Server Load:*2.49</li>
<li class="Fri">Fri 29th Jul, 03:30:04 am | Server Load:*3.79</li>

<li class="Fri">Fri 29th Jul, 04:30:05 am | Server Load:*4.52</li>
<li class="Fri">Fri 29th Jul, 05:30:07 am | Server Load:*4.49</li>
<li class="Fri">Fri 29th Jul, 06:30:04 am | Server Load:*3.08</li>
<li class="Fri">Fri 29th Jul, 07:30:04 am | Server Load:*2.81</li>
<li class="Fri">Fri 29th Jul, 10:30:05 am | Server Load:*2.90</li>
<li class="Fri">Fri 29th Jul, 11:30:05 am | Server Load:*2.39</li>

<li class="Fri">Fri 29th Jul, 12:30:04 pm | Server Load:*5.57</li>
<li class="Fri">Fri 29th Jul, 01:30:05 pm | Server Load:*4.04</li>
<li class="Fri">Fri 29th Jul, 02:30:04 pm | Server Load:*3.07</li>
<li class="Fri">Fri 29th Jul, 03:30:05 pm | Server Load:*3.08</li>
<li class="Fri">Fri 29th Jul, 04:30:12 pm | Server Load:*3.39</li>
<li class="Fri">Fri 29th Jul, 05:30:07 pm | Server Load:*8.80</li>

<li class="Fri">Fri 29th Jul, 06:30:04 pm | Server Load:*7.14</li>
<li class="Fri">Fri 29th Jul, 07:30:04 pm | Server Load:*2.72</li>
<li class="Fri">Fri 29th Jul, 08:30:04 pm | Server Load:*2.02</li>
<li class="Fri">Fri 29th Jul, 09:30:12 pm | Server Load:*2.82</li>
<li class="Fri">Fri 29th Jul, 10:30:08 pm | Server Load:*1.88</li>
<li class="Fri">Fri 29th Jul, 11:30:04 pm | Server Load:*2.05</li>

<li class="Sat">Sat 30th Jul, 12:30:04 am | Server Load:*2.38</li>
<li class="Sat">Sat 30th Jul, 02:30:04 am | Server Load:*4.20</li>
<li class="Sat">Sat 30th Jul, 03:30:04 am | Server Load:*6.55</li>
<li class="Sat">Sat 30th Jul, 04:30:04 am | Server Load:*3.50</li>
<li class="Sat">Sat 30th Jul, 05:30:06 am | Server Load:*3.11</li>
<li class="Sat">Sat 30th Jul, 06:30:04 am | Server Load:*5.17</li>

<li class="Sat">Sat 30th Jul, 07:30:04 am | Server Load:*4.12</li>
<li class="Sat">Sat 30th Jul, 08:30:04 am | Server Load:*3.94</li>
<li class="Sat">Sat 30th Jul, 09:30:05 am | Server Load:*3.32</li>
<li class="Sat">Sat 30th Jul, 10:30:05 am | Server Load:*3.47</li>
<li class="Sat">Sat 30th Jul, 11:30:04 am | Server Load:*3.07</li>
<li class="Sat">Sat 30th Jul, 12:30:04 pm | Server Load:*5.41</li>

<li class="Sat">Sat 30th Jul, 01:30:05 pm | Server Load:*2.88</li>
<li class="Sat">Sat 30th Jul, 02:30:04 pm | Server Load:*3.11</li>
<li class="Sat">Sat 30th Jul, 03:30:04 pm | Server Load:*2.61</li>
<li class="Sat">Sat 30th Jul, 04:30:07 pm | Server Load:*1.99</li>
<li class="Sat">Sat 30th Jul, 05:30:06 pm | Server Load:*2.48</li>
<li class="Sat">Sat 30th Jul, 06:30:04 pm | Server Load:*2.59</li>

<li class="Sat">Sat 30th Jul, 07:30:05 pm | Server Load:*2.11</li>
<li class="Sat">Sat 30th Jul, 08:30:05 pm | Server Load:*1.76</li>
<li class="Sat">Sat 30th Jul, 09:30:04 pm | Server Load:*1.81</li>
<li class="Sat">Sat 30th Jul, 10:30:04 pm | Server Load:*2.34</li>
<li class="Sat">Sat 30th Jul, 11:30:04 pm | Server Load:*2.22</li>
<li class="Sun">Sun 31st Jul, 12:30:04 am | Server Load:*3.40</li>

<li class="Sun">Sun 31st Jul, 01:30:31 am | Server Load:*3.04</li>
<li class="Sun">Sun 31st Jul, 02:30:04 am | Server Load:*3.49</li>
<li class="Sun">Sun 31st Jul, 03:30:04 am | Server Load:*3.52</li>
<li class="Sun">Sun 31st Jul, 04:30:04 am | Server Load:*3.23</li>
<li class="Sun">Sun 31st Jul, 05:30:10 am | Server Load:*2.46</li>
<li class="Sun">Sun 31st Jul, 06:30:04 am | Server Load:*3.56</li>

<li class="Sun">Sun 31st Jul, 07:30:03 am | Server Load:*4.17</li>
<li class="Sun">Sun 31st Jul, 08:30:04 am | Server Load:*4.59</li>
<li class="Sun">Sun 31st Jul, 09:30:04 am | Server Load:*4.29</li>
<li class="Sun">Sun 31st Jul, 10:30:04 am | Server Load:*4.11</li>
<li class="Sun">Sun 31st Jul, 11:30:04 am | Server Load:*3.49</li>
<li class="Sun">Sun 31st Jul, 12:30:04 pm | Server Load:*4.77</li>

<li class="Sun">Sun 31st Jul, 01:30:04 pm | Server Load:*3.47</li>
<li class="Sun">Sun 31st Jul, 02:30:03 pm | Server Load:*2.93</li>
<li class="Sun">Sun 31st Jul, 03:30:04 pm | Server Load:*2.28</li>
<li class="Sun">Sun 31st Jul, 04:30:04 pm | Server Load:*3.22</li>
<li class="Sun">Sun 31st Jul, 05:30:05 pm | Server Load:*3.23</li>
<li class="Sun">Sun 31st Jul, 06:30:04 pm | Server Load:*3.97</li>

<li class="Sun">Sun 31st Jul, 08:30:19 pm | Server Load:*5.09</li>
<li class="Sun">Sun 31st Jul, 09:30:05 pm | Server Load:*4.92</li>
<li class="Sun">Sun 31st Jul, 10:30:07 pm | Server Load:*3.97</li>
<li class="Sun">Sun 31st Jul, 11:30:04 pm | Server Load:*11.19</li>

Nice. I don't know whether it was the c&p or if it was the forums here, but there is no linefeeds between these.

One other benefit is that the packed file is 6kb versus the original @ 49kb.

I love random access files.

08-29-2011, 12:58 AM
$file = './newseek.dat';

$lines = file( $file, FILE_IGNORE_NEW_LINES );
$lines = array_slice( $lines, -100 );