Created
August 4, 2020 14:41
-
-
Save abdmun8/68de64edc60b2bdd23b0de66cb936775 to your computer and use it in GitHub Desktop.
Delete file older than 7 days using php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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