Re: Read Last Lines of a Text File

From: Date: Thu, 17 Jun 2004 00:39:18 +0000
Subject: Re: Read Last Lines of a Text File
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-188449@lists.php.net to get a copy of this message
* Thus wrote Scott Miller (srmiller@interbel.net): > 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!

« previous php.general (#188449) next »