-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete-old-files.php
28 lines (22 loc) · 953 Bytes
/
delete-old-files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
$folderName = __DIR__ . '/audio';
if (file_exists($folderName)) {
$iterator = new RecursiveDirectoryIterator($folderName);
// Skip "dot" files
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
// Loop through files
foreach(new RecursiveIteratorIterator($iterator) as $file) {
if ($file->isFile() && ($file->getExtension() == 'mp3' || $file->getExtension() == 'zip') && time() - $file->getCTime() >= 30*24*60*60) {
unlink($file->getRealPath());
}
}
// Get directories only
$directories = new ParentIterator($iterator);
// Loop over directories and remove empty ones
foreach (new RecursiveIteratorIterator($directories, RecursiveIteratorIterator::SELF_FIRST) as $dir) {
// Count the number of "children" from the main directory iterator
if (iterator_count($iterator->getChildren()) === 0) {
rmdir($dir->getPathname());
}
}
}