Reading 1 character at a time is killing you.
I don't know how large is quite large, but this should be way faster:
<?php
function total($filename){
$total = 0;
$file = fopen($filename, 'r') or die("Could not open $filename");
while (!feof($file)){
$line = fgets($file, 1000000);
$fields = explode('|', $line);
# The array is 0-based, so 14 *should* be 15th field...
$total += $fields[14];
}
fclose($file);
}
?>
You may still need to add a set_time_limit(30) in the loop if the file is
truly massive...
--
Visit the Zend Store at http://www.zend.com/store/
Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm
----- Original Message -----
From: "Abe Asghar" <[EMAIL PROTECTED]>
Newsgroups: php.general
Sent: Monday, February 19, 2001 9:21 AM
Subject: [PHP] <? -- File Total -- ?>
Hi,
I am trying to get a total value of orders from a file. The file has
various fields separated by '|'. It is the 15th field I am trying to total.
At the moment I am trying to search each character but it times out as the
file is quite large - is there a more efficient way of doing this.
Thanks - Here is the script:
<?php
function total($filename)
{ $totalfile=16;
$total=0;
$vartemp="";
$counterbeforeend=0;
$counter=0;
if(!$file=fopen($filename,"r"))
{
echo"Could not open the file named '$filename'";
}
else
while((!feof($file))&&($total<1000))
{
$char=fgetc($file);
if(strcmp($char,"|")==0)
$counter++;
if($counter==($totalfile+1))
{
$vartemp=$vartemp.$char;
if((strcmp($char,".")==0)||($passed==1))
{
$counterbeforeend++;
$passed=1;
}
if($counterbeforeend==3)
{
$total=$total+$vartemp;
$passed=0;
$counter=0;
$counterbeforeend=0;
$vartemp="";
echo"$total<BR>\n";
}
}
else if($counter==$totalfile)
{
$counter++;
}
}
}
return($total);
}
?>
<HTML>
<BODY>
<?
$total1=total("G&V_orderslist.txt");
echo"$total1";
?>
</BODY>
</HTML>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]