Reputation: 607
I'm using scandir to get a list of all of the access logs in the logs directory and can display the different months of access logs if the $access_log
= certain criteria which is based on domain and month.
What I want to do is simplify this code so I don't have to write out all of these if statements and month variables.
For instance if there are 100 access logs for each domain I'd have to write out all these if statements 100 times.
An access log looks like this subdomain.mydomain.com-Jan-2016.gz
.
So I'm trying to figure out how to rewrite my code to have only 1 if statement that looks for a pattern of a $domain
in the $access_log
and lists those access logs in the $log_month
variable. So far can't figure it out unless I write a separate if statement for each $log_month
variable as you can see below in the 3 if statement examples within the foreach loop.
$access_logs = scandir($log_list);
$month = date("M-Y");
$prevmonth = date('M-Y', strtotime("first day of last month"));
$minus_2_month = date('M-Y', strtotime("first day of -2 month"));
$minus_3_month = date('M-Y', strtotime("first day of -3 month"));
$minus_4_month = date('M-Y', strtotime("first day of -4 month"));
//etc etc etc
foreach($access_logs as $access_log) {
if ($access_log == $domain_dashes . '.mydomain.com-' . $month . '.gz') {
$log_month = '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n ";
}
if ($access_log == $domain_dashes . '.mydomain.com-' . $prevmonth . '.gz') {
$log_month .= '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n ";
}
if ($access_log == $domain_dashes . '.mydomain.com-' . $minus_2_month . '.gz') {
$log_month .= '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n ";
}
// etc etc etc
}
Update Jan 19 2016
To be more specific, this is what I need. A list of domains with the access logs next to each domain.
Domain 1 - domain1.com-jan-2016.gz | domain1.com-dec-2015.gz
Domain 2 - domain2.com-jan-2016.gz
Domain 3 - domain3.com-jan-2016.gz | domain3.com-dec-2015.gz | etc | etc
etc
etc
next to each domain I need it to display the access log files. For domain 1 there might be 2 or 3 files from various months. For domain 2 there might be only 1 file. For domain 3 there might be a dozen.
The way I have my code above works for what I need. But I wanted to condense the code with || or one simple if statement, but when I do that, then not all the access logs get displayed next to each domain. Only one file will get displayed.
How do I get all the access log files next to each domain name with a condensed version of my code above?
Upvotes: 0
Views: 1071
Reputation: 1332
You can use the PHP glob function instead of using scandir. With glob you can get pathnames by a pattern.
For example:
$access_logs = glob('*.mydomain.com-*.gz');
$log_month = '';
foreach($access_logs as $access_log) {
$log_month .= '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n ";
}
Update Jan 19 2016
I recommend to use regex to split the infomation (domain, month, year) from the filename. To collect all log files per domain you can use the domain as array key. After gathering you have to loop through all domains and you can list all corresponding log files.
<?php
# gathering
$access_logs = glob('*.gz');
$domainList = [];
foreach ($access_logs as $access_log) {
if (preg_match('/((?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6})-(\w{3})-(\d{4})/i', $access_log, $matches)) {
$domain = $matches[1];
# $month = $matches[2];
# $year = $matches[3];
if (!isset($domainList[$domain])) {
$domainList[$domain] = '';
}
$domainList[$domain] .= '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>';
}
}
# output
foreach ($domainList as $domain => $list) {
echo "<h1>{$domain}</h1>";
echo "<ul>{$list}</ul>";
}
You can adjust the output by your own. But the logic do what you want.
Upvotes: 1