Re: Getting line count of a text file
| From: | Per Jessen | Date: | Wed, 12 Sep 2007 20:23:42 +0000 |
| Subject: | Re: Getting line count of a text file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-262052@lists.php.net to get a copy of this message | ||
Frank J. Schima wrote:
> In PHP 5, I'm counting the number of lines in a text file using the
> following code:
> $myfile = file ( '/path/to/myfile.txt');
> $count = count ($myfile);
>
> However, sometimes it fails with the following error:
> PHP Fatal error: Allowed memory size of xxxx bytes exhausted (tried
> to allocate 35 bytes) in /path/to/myprogram.php on line 16
>
> Line 16 is the first line above.
>
> I'm sure there is a better - more memory efficient - way to do this.
> Any suggestions?
$f=fopen( yourfile, "r" );
$i=0;
while( fgets($f) ) $i++;
fclose($f);
$count=$i;
/Per Jessen, Zürich