Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Scott Miller

 * Thus wrote Scott Miller ([EMAIL PROTECTED]):
  I have a text file (log file) that I want to be able to read the last 30
or
  40 lines of.  I've created the code below, but since this file is so
large
  (currently 8 MB) it won't work.  The code works on smaller files, but
not
  this one.  Can someone look at this below and tell me where I went
wrong?
 
  ...
 
  $fcontents = file($filename);

 This will make your script consume lots of memory, and is very
 inefficient.

 You'd be better of using the unix tail command:

 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
   echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
   $line = fgets($fp);
   print $line;
 }

 Of course if you're simply going to output the results a simple:

   system(/usr/bin/tail -$limit $file);

 Would do the job nicely.


 Curt
 -- 
 First, let me assure you that this is not one of those shady pyramid
schemes
 you've been hearing about.  No, sir.  Our model is the trapezoid!

 -- 

I've changed my script to the following:

?php

$file =/var/log/radius.log;

$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
  echo 'unable to pipe command';
}
while (!feof($fp) ) {
  $line = fgets($fp);
  print $line;
}
?

And now get the following errors:

Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php on
line 10

I get this over and over and over (like it's producing the error once per
line in the log file).

Thanks,
Scott



-- 
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.251 / Virus Database: 263.3.2 - Release Date: 6/15/2004

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Oliver Hankeln
Scott Miller wrote:
* Thus wrote Scott Miller ([EMAIL PROTECTED]):
I have a text file (log file) that I want to be able to read the last 30
or
40 lines of.  I've created the code below, but since this file is so
large
(currently 8 MB) it won't work.  The code works on smaller files, but
not
this one.  Can someone look at this below and tell me where I went
wrong?
...
$fcontents = file($filename);
This will make your script consume lots of memory, and is very
inefficient.
You'd be better of using the unix tail command:
$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
 echo 'unable to pipe command';
}
while (!feof($fp) ) {
 $line = fgets($fp);
 print $line;
}
Of course if you're simply going to output the results a simple:
 system(/usr/bin/tail -$limit $file);
Would do the job nicely.
Curt
--
First, let me assure you that this is not one of those shady pyramid
schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!
--

I've changed my script to the following:
?php
$file =/var/log/radius.log;
$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
  echo 'unable to pipe command';
}
while (!feof($fp) ) {
  $line = fgets($fp);
  print $line;
}
?
And now get the following errors:
Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php on
line 10
I get this over and over and over (like it's producing the error once per
line in the log file).
prior to PHP 4.2 you had to use a second parameter for fgets. This 
parameter is the maximum length per line to read.
$line = fget($fp,4096);
will probably work.

HTH,
Oliver
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Scott Miller

- Original Message - 
From: Oliver Hankeln [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, June 17, 2004 9:13 AM
Subject: Re: [PHP] Read Last Lines of a Text File


 Scott Miller wrote:

 * Thus wrote Scott Miller ([EMAIL PROTECTED]):
 
 I have a text file (log file) that I want to be able to read the last
30
 
  or
 
 40 lines of.  I've created the code below, but since this file is so
 
  large
 
 (currently 8 MB) it won't work.  The code works on smaller files, but
 
  not
 
 this one.  Can someone look at this below and tell me where I went
 
  wrong?
 
 ...
 
 $fcontents = file($filename);
 
 This will make your script consume lots of memory, and is very
 inefficient.
 
 You'd be better of using the unix tail command:
 
 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
   echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
   $line = fgets($fp);
   print $line;
 }
 
 Of course if you're simply going to output the results a simple:
 
   system(/usr/bin/tail -$limit $file);
 
 Would do the job nicely.
 
 
 Curt
 -- 
 First, let me assure you that this is not one of those shady pyramid
 
  schemes
 
 you've been hearing about.  No, sir.  Our model is the trapezoid!
 
 -- 
 
 
  I've changed my script to the following:
 
  ?php
 
  $file =/var/log/radius.log;
 
  $fp = popen(/usr/bin/tail -$limit $file, 'r');
  if (! $fp ) {
echo 'unable to pipe command';
  }
  while (!feof($fp) ) {
$line = fgets($fp);
print $line;
  }
  ?
 
  And now get the following errors:
 
  Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php on
  line 10
 
  I get this over and over and over (like it's producing the error once
per
  line in the log file).

 prior to PHP 4.2 you had to use a second parameter for fgets. This
 parameter is the maximum length per line to read.
 $line = fget($fp,4096);
 will probably work.

 HTH,
 Oliver

Changed that line as shown above, and now get the following error:

Fatal error: Call to undefined function: fget() in /var/www/html/logs2.php
on line 11

Thanks,
Scott



-- 
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.251 / Virus Database: 263.3.2 - Release Date: 6/15/2004

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Oliver Hankeln
Scott Miller wrote:
I've changed my script to the following:
?php
$file =/var/log/radius.log;
$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
 echo 'unable to pipe command';
}
while (!feof($fp) ) {
 $line = fgets($fp);
 print $line;
}
?
And now get the following errors:
Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php on
line 10
I get this over and over and over (like it's producing the error once
per
line in the log file).
prior to PHP 4.2 you had to use a second parameter for fgets. This
parameter is the maximum length per line to read.
$line = fget($fp,4096);
will probably work.
HTH,
Oliver

Changed that line as shown above, and now get the following error:
Fatal error: Call to undefined function: fget() in /var/www/html/logs2.php
on line 11
Sorry. I mean fgets ...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Scott Miller

- Original Message - 
From: Oliver Hankeln [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, June 17, 2004 9:29 AM
Subject: Re: [PHP] Read Last Lines of a Text File


 Scott Miller wrote:

 I've changed my script to the following:
 
 ?php
 
 $file =/var/log/radius.log;
 
 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
   echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
   $line = fgets($fp);
   print $line;
 }
 ?
 
 And now get the following errors:
 
 Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php
on
 line 10
 
 I get this over and over and over (like it's producing the error once
 
  per
 
 line in the log file).
 
 prior to PHP 4.2 you had to use a second parameter for fgets. This
 parameter is the maximum length per line to read.
 $line = fget($fp,4096);
 will probably work.
 
 HTH,
 Oliver
 
 
  Changed that line as shown above, and now get the following error:
 
  Fatal error: Call to undefined function: fget() in
/var/www/html/logs2.php
  on line 11

 Sorry. I mean fgets ...

That worked perfectly - now I at least get this:

== standard input == == /var/log/radius.log == Thu Jun 17 09:21:01 2004:
Auth: Login OK: [flasht999] (from nas Cisco AS5300/S0) socket 0 (0 sec) Thu
Jun 17 09:22:22 2004: Auth: Login OK: [crbm] (from nas Cisco AS5300/S0)
socket 0 (0 sec) Thu Jun 17 09:22:23 2004: Auth: Login OK: [tleec] (from nas
Cisco AS5300/S0) socket 0 (0 sec) Thu Jun 17 09:23:19 2004: Auth: Login OK:
[ziegen] (from nas Cisco AS5300/S0) socket 0 (0 sec) Thu Jun 17 09:23:24
2004: Auth: Login OK: [ziegen] (from nas 10.1.4.21/S0) socket 0 (0 sec) Thu
Jun 17 09:23:42 2004: Auth: Login OK: [indiantrails] (from nas Cisco
AS5300/S0) socket 0 (0 sec) Thu Jun 17 09:24:38 2004: Auth: Login OK:
[kdglover] (from nas Cisco AS5300/S0) socket 0 (0 sec) Thu Jun 17 09:25:39
2004: Auth: Login OK: [altron] (from nas Cisco AS5300/S0) socket 0 (0 sec)
Thu Jun 17 09:26:06 2004: Auth: Login OK: [altron] (from nas 10.1.4.21/S0)
socket 0 (0 sec) Thu Jun 17 09:26:37 2004: Auth: Login OK: [nc3] (from nas
10.1.4.21/S0) socket 0 (0 sec)

Now, how can I force a \n; to make it look like this:

== standard input ==
== /var/log/radius.log ==
Thu Jun 17 09:21:01 2004: Auth: Login OK: [flasht999] (from nas Cisco
AS5300/S0) socket 0 (0 sec)
Thu Jun 17 09:22:22 2004: Auth: Login OK: [crbm] (from nas Cisco AS5300/S0)
socket 0 (0 sec)
Thu Jun 17 09:22:23 2004: Auth: Login OK: [tleec] (from nas Cisco AS5300/S0)
socket 0 (0 sec)
Thu Jun 17 09:23:19 2004: Auth: Login OK: [ziegen] (from nas Cisco
AS5300/S0) socket 0 (0 sec)
Thu Jun 17 09:23:24 2004: Auth: Login OK: [ziegen] (from nas xx.xx.xx.xx/S0)
socket 0 (0 sec)
Thu Jun 17 09:23:42 2004: Auth: Login OK: [indiantrails] (from nas Cisco
AS5300/S0) socket 0 (0 sec)
Thu Jun 17 09:24:38 2004: Auth: Login OK: [kdglover] (from nas Cisco
AS5300/S0) socket 0 (0 sec)
Thu Jun 17 09:25:39 2004: Auth: Login OK: [altron] (from nas Cisco
AS5300/S0) socket 0 (0 sec)
Thu Jun 17 09:26:06 2004: Auth: Login OK: [altron] (from nas xx.xx.xx.xx/S0)
socket 0 (0 sec)
Thu Jun 17 09:26:37 2004: Auth: Login OK: [nc3] (from nas xx.xx.xx.xx/S0)
socket 0 (0 sec)

Thanks,
Scott



-- 
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.251 / Virus Database: 263.3.2 - Release Date: 6/15/2004

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Daniel Clark
Try adding number of bytes to read.

   $line = fgets($fp, 4096);


 * Thus wrote Scott Miller ([EMAIL PROTECTED]):
  I have a text file (log file) that I want to be able to read the last
 30
 or
  40 lines of.  I've created the code below, but since this file is so
 large
  (currently 8 MB) it won't work.  The code works on smaller files, but
 not
  this one.  Can someone look at this below and tell me where I went
 wrong?
 
  ...
 
  $fcontents = file($filename);

 This will make your script consume lots of memory, and is very
 inefficient.

 You'd be better of using the unix tail command:

 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
   echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
   $line = fgets($fp);
   print $line;
 }

 Of course if you're simply going to output the results a simple:

   system(/usr/bin/tail -$limit $file);

 Would do the job nicely.


 Curt
 --
 First, let me assure you that this is not one of those shady pyramid
 schemes
 you've been hearing about.  No, sir.  Our model is the trapezoid!

 --

 I've changed my script to the following:

 ?php

 $file =/var/log/radius.log;

 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
   echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
   $line = fgets($fp);
   print $line;
 }
 ?

 And now get the following errors:

 Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php on
 line 10

 I get this over and over and over (like it's producing the error once per
 line in the log file).

 Thanks,
 Scott

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Oliver Hankeln
Scott Miller wrote:
- Original Message - 
From: Oliver Hankeln [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, June 17, 2004 9:29 AM
Subject: Re: [PHP] Read Last Lines of a Text File


Scott Miller wrote:

I've changed my script to the following:
?php
$file =/var/log/radius.log;
$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
echo 'unable to pipe command';
}
while (!feof($fp) ) {
$line = fgets($fp);
print $line;
}
?
And now get the following errors:
Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php
on
line 10
I get this over and over and over (like it's producing the error once
per

line in the log file).
prior to PHP 4.2 you had to use a second parameter for fgets. This
parameter is the maximum length per line to read.
$line = fget($fp,4096);
will probably work.
HTH,
Oliver

Changed that line as shown above, and now get the following error:
Fatal error: Call to undefined function: fget() in
/var/www/html/logs2.php
on line 11
Sorry. I mean fgets ...
That worked perfectly - now I at least get this:
[snip]
Now, how can I force a \n; to make it look like this:
Simply add one. print $line.\n; should work. Or if your output is html 
print $line.br;

Oliver
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Scott Miller

- Original Message - 
From: Oliver Hankeln [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Thursday, June 17, 2004 10:01 AM
Subject: Re: [PHP] Read Last Lines of a Text File


 Scott Miller wrote:

  - Original Message - 
  From: Oliver Hankeln [EMAIL PROTECTED]
  To: [EMAIL PROTECTED]
  Sent: Thursday, June 17, 2004 9:29 AM
  Subject: Re: [PHP] Read Last Lines of a Text File
 
 
 
 Scott Miller wrote:
 
 
 I've changed my script to the following:
 
 ?php
 
 $file =/var/log/radius.log;
 
 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
  echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
  $line = fgets($fp);
  print $line;
 }
 ?
 
 And now get the following errors:
 
 Warning: Wrong parameter count for fgets() in /var/www/html/logs2.php
 
  on
 
 line 10
 
 I get this over and over and over (like it's producing the error once
 
 per
 
 
 line in the log file).
 
 prior to PHP 4.2 you had to use a second parameter for fgets. This
 parameter is the maximum length per line to read.
 $line = fget($fp,4096);
 will probably work.
 
 HTH,
 Oliver
 
 
 Changed that line as shown above, and now get the following error:
 
 Fatal error: Call to undefined function: fget() in
 
  /var/www/html/logs2.php
 
 on line 11
 
 Sorry. I mean fgets ...
 
 
  That worked perfectly - now I at least get this:
 
 [snip]
  Now, how can I force a \n; to make it look like this:

 Simply add one. print $line.\n; should work. Or if your output is html
 print $line.br;

 Oliver

The following works perfectly:

?php

$file =/var/log/radius.log;


$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
  echo 'unable to pipe command';
}
while (!feof($fp) ) {
   $line = fgets($fp, 4096);
print $line.br;


}
?

I've tried bumping up the 4096 to a higher number because it currently only
shows me 10 lines - I'd like to see about 30.  No matter what I change the
number to, it still only shows 10 lines.  What do I need to do to get more
that the 10 lines?

Thanks,
Scott



-- 
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.251 / Virus Database: 263.3.2 - Release Date: 6/15/2004

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Matt Matijevich
[snip]
?php

$file =/var/log/radius.log;


$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
  echo 'unable to pipe command';
}
while (!feof($fp) ) {
   $line = fgets($fp, 4096);
print $line.br;


}
?

I've tried bumping up the 4096 to a higher number because it currently
only
shows me 10 lines - I'd like to see about 30.  No matter what I change
the
number to, it still only shows 10 lines.  What do I need to do to get
more
that the 10 lines?
[/snip]

I am jumping in late so I might have missed it.  What is $limit set
too?  is it set to 30?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-17 Thread Scott Miller


 [snip]
 ?php
 
 $file =/var/log/radius.log;
 
 
 $fp = popen(/usr/bin/tail -$limit $file, 'r');
 if (! $fp ) {
   echo 'unable to pipe command';
 }
 while (!feof($fp) ) {
$line = fgets($fp, 4096);
 print $line.br;
 
 
 }
 ?
 
 I've tried bumping up the 4096 to a higher number because it currently
 only
 shows me 10 lines - I'd like to see about 30.  No matter what I change
 the
 number to, it still only shows 10 lines.  What do I need to do to get
 more
 that the 10 lines?
 [/snip]
 
 I am jumping in late so I might have missed it.  What is $limit set
 too?  is it set to 30?
 

I actually don't have limit set anywhere - I added:

$limit=30;

directly under $file = ... and got exactly what I need.

Thanks for all the help!

Scott




-- 
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.251 / Virus Database: 263.3.2 - Release Date: 6/15/2004

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-16 Thread Matt Matijevich
[snip]
I've created the code below, but since this file is so large
(currently 8 MB) it won't work.
[/snip]

I would guess you are running into your php memory_limit.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-16 Thread Matt Grimm
[EMAIL PROTECTED] (Scott Miller) wrote in
news:[EMAIL PROTECTED]: 

 I have a text file (log file) that I want to be able to read the last
 30 or 40 lines of.  I've created the code below, but since this file
 is so large (currently 8 MB) it won't work.  The code works on smaller
 files, but not this one.  Can someone look at this below and tell me
 where I went wrong? 
 
 ?php
 
 $filename =/var/log/radius.log;
 
 $myFile = fopen($filename, r); //open the file for reading, file
 pointer will be at the beginning of the file
 
 if(! $myFile){// Make sure the file was opened
 successfully 
 
 print (File could not be opened.);
 
 exit;
 }
 
 $fcontents = file($filename);
 
   $limit = 30;
 
 for ($i = 0; $i  $limit; $i++){ // while $i is less than 30
 
 $line = $fcontents[$i];  // assign value of array element to a
 variable 
 
   if($line != ){  // if the line from the file is not blank
   print it 
 echo $line \n;
   }
 
  }
 
 fclose($myFile);
 
 ?
 
 Thanks,
 Scott Miller
 
 
 

Your problem is likely with the following line, which reads the *entire* 
file into an array:

$fcontents = file($filename);

You're also opening and closing a file handle, but never using it. Use 
fread after fopen instead of the file function.  You'll have to decide on 
a byte limit instead of a line limit, though.

http://php.net/manual/en/function.fread.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Read Last Lines of a Text File

2004-06-16 Thread Ashwin Purohit
Or maybe execution time limit.
[snip]
I've created the code below, but since this file is so large
(currently 8 MB) it won't work.
[/snip]
I would guess you are running into your php memory_limit.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
_
Is your PC infected? Get a FREE online computer virus scan from McAfee® 
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Read Last Lines of a Text File

2004-06-16 Thread Curt Zirzow
* Thus wrote Scott Miller ([EMAIL PROTECTED]):
 I have a text file (log file) that I want to be able to read the last 30 or
 40 lines of.  I've created the code below, but since this file is so large
 (currently 8 MB) it won't work.  The code works on smaller files, but not
 this one.  Can someone look at this below and tell me where I went wrong?

 ...
 
 $fcontents = file($filename);

This will make your script consume lots of memory, and is very
inefficient.

You'd be better of using the unix tail command:

$fp = popen(/usr/bin/tail -$limit $file, 'r');
if (! $fp ) {
  echo 'unable to pipe command';
}
while (!feof($fp) ) {
  $line = fgets($fp);
  print $line;
}

Of course if you're simply going to output the results a simple:

  system(/usr/bin/tail -$limit $file);

Would do the job nicely.


Curt
-- 
First, let me assure you that this is not one of those shady pyramid schemes
you've been hearing about.  No, sir.  Our model is the trapezoid!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php