Extracting data from a txt file in php
Posted: Fri May 10, 2013 12:32 pm
I have a log file which is a .txt file which displays
IP ADDRESS, TIME STAMP,FILENAME,HTTP STATUS CODE,BANDWIDTH,USER AGENT
1.)103.239.234.105 -- [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 200 12729 "Mozilla/4.0"
2.)207.3.35.52 -- [2007-04-01 01:24:42] "GET index.php HTTP/1.0" 200 11411 "Mozilla/4.0"
I need to findout
1. The total number of file requests in the month.
2. The number of file requests from the articles directory.
3. The TOTAL bandwidth consumed by the file requests over the month.
4. The number of requests that resulted in 404 status errors. Display a list of the filenames that produced these 404 errors (try not to repeat filenames if the same wrong filename was requested more than once
i've managed to get the total number of files
<?php
$file="april.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo $linecount;
?>
to get the data i know i need to explode the strings into an array and loop through the array to count the information needed but i'm struggling with exploding it properly so it breaks up the array properly
any ideas
IP ADDRESS, TIME STAMP,FILENAME,HTTP STATUS CODE,BANDWIDTH,USER AGENT
1.)103.239.234.105 -- [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 200 12729 "Mozilla/4.0"
2.)207.3.35.52 -- [2007-04-01 01:24:42] "GET index.php HTTP/1.0" 200 11411 "Mozilla/4.0"
I need to findout
1. The total number of file requests in the month.
2. The number of file requests from the articles directory.
3. The TOTAL bandwidth consumed by the file requests over the month.
4. The number of requests that resulted in 404 status errors. Display a list of the filenames that produced these 404 errors (try not to repeat filenames if the same wrong filename was requested more than once
i've managed to get the total number of files
<?php
$file="april.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo $linecount;
?>
to get the data i know i need to explode the strings into an array and loop through the array to count the information needed but i'm struggling with exploding it properly so it breaks up the array properly
any ideas