指定時間経過したログファイルやキャッシュファイルを削除
exec('find ./chache -cmin +240 -name "*" | xargs rm -f');
PHPファイルと同じ階層にある、「chache」フォルダ配下に作成された全てのファイルのうち、240分(4時間)以上経過したものを削除しています。
ホスティングサーバーのセキュリティレベルによりexecが利用できない場合は以下のようなコードになります。
foreach(glob('./chache/*') as $file) {
$time1 = new DateTime(date('Y-m-d H:i:s',filemtime($file)));
$time2 = new DateTime();
$interval = ($time2->getTimestamp() - $time1->getTimestamp()) / 60;
if($interval > 240) {
unlink($file);
}
}
