// 1- get a list of file in the CI default CI log file
$LogPathCI = '/var/www/html/Zapplication/logs';
$f1 = new FilesystemIterator($LogPathCI , FilesystemIterator::SKIP_DOTS);
$gjcount=0; // start at
$gjstart=10; // total number to keep
if(iterator_count($f1) < $gjstart){
gjLog('Number of Files less than number to keep - Nothing to unlink' .PHP_EOL,'test.log');
}
// 2- create an array of files to sort by Modified Time in step 3
$thefiles = array();
foreach($f1 as $fileinfo) {
$akey = $f1->getFileName();
if((strtolower($akey)) != 'index.html') {
$thefiles[$akey] = $f1->getMTime();
}
}
// 3- sort the files by date - descending newest first
arsort($thefiles);
// 4- start at 0, after 10 files delete the rest
foreach ($thefiles as $key => $value) {
if($gjstart < $gjcount){
gjLog('unlink these key: ' . $key .' value: '. $value .PHP_EOL,'test.log');
$killme = $LogPathCI . DIRECTORY_SEPARATOR . $key;
if(!unlink($killme)) {
gjLog('unable to unlink '. $killme .PHP_EOL,'test.log');
}
} else {
gjLog('keep these key: ' . $key .' value: '. $value .PHP_EOL,'test.log');
}
$gjcount++;
}