user3350731
user3350731

Reputation: 972

How to analyze multiple logfiles

I have a folder called awstats which contains all the log files for each month, I need to parse all of them and get content for all the files at the same time

I tried this :

$filename = DOCUMENT_ROOT."sites/".HOSTNAME."/awstats/".$_SESSION['awstat_month'].$_SESSION['awstat_year']."."HOSTNAME".txt";
if(file_exists($filename))
    $awstats = file_get_contents($filename) ; 
else {
    print_warning("the log file does not exist!");
    return;
}

But it only parse a log file at the time. Is there a way to parse all the log file at the same time and get a certain content for each file? Much Appreciated!

Upvotes: 0

Views: 137

Answers (2)

hgre
hgre

Reputation: 51

You should in some way iterate over those statistic files. It seems those variable parameters are @ $_SESSION['awstat_mont'] and $_SESSION['awstat_year'] so a naive solution would be a double foreach loop: (I am assuming you are using php so have a look over here: http://de3.php.net/manual/en/control-structures.foreach.php)

foreach ($allYearsToCheck as $year) {
   foreach ($allMonthToCheck as $month) {
      $filename =DOCUMENT_ROOT."sites/".HOSTNAME."/awstats/".$month.$year."."HOSTNAME".txt";
      if(file_exists($filename)){
         $awstats .= file_get_contents($filename) ; 
      }
      else {
         print_warning("the log file does not exist!"
      }
   }
}

where allMontToCheck and allYearsToCheck are arrays containig all years and month you would like to check. $awstats will contain all logfiles concatenated.

Upvotes: 1

ncrocfer
ncrocfer

Reputation: 2570

You can use the glob function :

<?php

foreach (glob(DOCUMENT_ROOT."sites/".HOSTNAME."/awstats/*.txt") as $filename) {

    $awstats = file_get_contents($filename) ;
    // do what you want with the content

}

Upvotes: 2

Related Questions