Skip to content

Instantly share code, notes, and snippets.

@abdmun8
Created August 4, 2020 14:41
Show Gist options
  • Save abdmun8/68de64edc60b2bdd23b0de66cb936775 to your computer and use it in GitHub Desktop.
Save abdmun8/68de64edc60b2bdd23b0de66cb936775 to your computer and use it in GitHub Desktop.
Delete file older than 7 days using php
<?php
// save in directory where file exists
$dir = __DIR__;
$files = scandir($dir);
$date = date_create();
$limit = strtotime('-7 day', date_timestamp_get($date));
$total = 0;
$count = 0;
foreach ($files as $key => $file) {
$total++;
$created = filemtime($file);
if ($file == 'index.php' || $file == '.' || $file == '..') {
continue;
}
// echo date('Y-m-d', $created)." < ".date('Y-m-d', $limit)."\n";
if ($created < $limit) {
unlink($file);
$count++;
}
}
// echo $count . " Files Deleted of " . $total;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment