-
Notifications
You must be signed in to change notification settings - Fork 9
/
clean_older.php
63 lines (54 loc) · 1.35 KB
/
clean_older.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
$old = 30*24*3600; //clean 30 days old files
function deltree($path) {
global $old;
$delete_this = 1;
if (is_dir($path)) {
if (version_compare(PHP_VERSION, '5.0.0') < 0) {
$entries = array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) $entries[] = $file;
closedir($handle);
}
} else {
$entries = @scandir($path);
if ($entries === false) $entries = array(); // just in case scandir fail...
}
foreach ($entries as $entry) {
if ($entry != '.' && $entry != '..') {
if (deltree($path.'/'.$entry)==-1) $delete_this=0;
}
}
if ($delete_this) if (time()-filemtime($path)> $old) return @rmdir($path);
else return -1;
} else {
if ($delete_this) if (time()-filemtime($path)> $old) return @unlink($path);
else return -1;
}
return -1;
}
function cleanUp($dir)
{
global $old;
echo "<div class='ui segment'><small>Cleaning files older than 30 days ...";
$k=0;
$handle=opendir($dir);
while (($file = readdir($handle))!==false)
{
if (($file != ".") && ($file != ".."))
{
if (is_dir("$dir/" . $file)) deltree($dir."/".$file);
elseif (time()-filemtime("$dir/" . $file)> $old) @unlink("$dir/" . $file);
$k++;
if ($k%50==0)
{
echo " .";
flush();
}
}
}
closedir($handle);
echo "</small></div>";
}
cleanUp('uploads');
?>