counterhit

fread how to Count total from Counter text


hey i have a counter text here and i need to know how to calculate the total this is my information

$filename = "data.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); $expode = explode("\n",$contents);

/** output 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 1024 */ I i need to calculate the total by exploding "\n" so i will output 12288 need to understand how to do this i have done this

foreach ($expode as $v) { $total = $total + $v;

echo $total;

} i did not get good results with this


Solution

  • <?php
    
        /**
         * @author Saxtor Inc
         * @copyright 2010
         */
    
        function TotalBytes($definefilename)
        {  
             $isfile     = is_file($definefilename); //define the file name
    
                if ($isfile == 1) //if the file is true
             {
                $handle   = fopen($definefilename, "r"); //open the file check the information
                $contents = fread($handle, filesize($definefilename)); //define the filesize
                $expode   = explode("\n", $contents); //explode the "\n char"
                $count    = count($expode, COUNT_RECURSIVE); //count how much total
                while ($i <= $count) //count total values
    
                {
                    $totalcount = $totalcount + $expode[$i] . "\n"; //count total values; :D
                    $i++;
                }
                return $totalcount; //return total aye it works
    
            }
            else
            {
                return "Not Found";
            }
        }
        echo TotalBytes('data.txt');
    
    ?>