----- 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