Вот готовая функция
<?php
//--------------------------------
// FUNCTION TO TRUNCATE LOG FILES
//--------------------------------
function trim_log_to_length($path,$numHeaderRows,$numRowsToKeep){
$file = file($path);
$headerRows = array_slice($file,0,$numHeaderRows);
// if this file is long enough were we should be truncating it
if(count($file) - $numRowsToKeep > $numHeaderRows){
// figure out the rows we wanna keep
$dataRowsToKeep = array_slice($file,count($file)-$numRowsToKeep,$numRowsToKeep);
// write the file
$newFileRows = array_merge($headerRows,$dataRowsToKeep);
file_put_contents($path, implode($newFileRows));
}
}
?>