-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelete_old_files.php
More file actions
78 lines (65 loc) · 2.48 KB
/
delete_old_files.php
File metadata and controls
78 lines (65 loc) · 2.48 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$uploadDir = 'uploads/';
define('COLOR_RESET', "\033[0m");
define('COLOR_CYAN', "\033[36m");
define('COLOR_WHITE', "\033[37m");
define('COLOR_RED', "\033[31m");
function deleteOldFiles($dir)
{
$files = glob($dir . '*');
foreach ($files as $file) {
// 60 days in seconds = 60 * 24 * 60 * 60
if (is_file($file) && time() - filemtime($file) >= 60 * 24 * 60 * 60) {
unlink($file);
}
}
}
function formatFileSize($size)
{
if ($size < 1024) {
return $size . ' bytes';
} elseif ($size < 1048576) {
return round($size / 1024, 2) . ' KB';
} elseif ($size < 1073741824) {
return round($size / 1048576, 2) . ' MB';
} else {
return round($size / 1073741824, 2) . ' GB';
}
}
function displayTimeLeftForFiles($dir, $totalSeconds)
{
while (true) {
system('clear');
deleteOldFiles($dir); // Run deletion every loop
$files = glob($dir . '*');
echo COLOR_CYAN . str_pad("File", 40) . str_pad("Size", 15) . str_pad("Time Left", 30) . COLOR_RESET . "\n";
echo str_repeat("-", 85) . "\n";
foreach ($files as $file) {
if (is_file($file)) {
$fileAge = time() - filemtime($file);
$remainingTime = ($totalSeconds - $fileAge);
$fileSize = filesize($file);
$formattedSize = formatFileSize($fileSize);
$fileName = substr(basename($file), 0, 40);
if ($remainingTime > 0) {
$daysLeft = floor($remainingTime / (24 * 60 * 60));
$hoursLeft = floor(($remainingTime % (24 * 60 * 60)) / 3600);
$minutesLeft = floor(($remainingTime % 3600) / 60);
$secondsLeft = $remainingTime % 60;
echo sprintf("%s%-40s %-15s %d days, %d hrs, %d min, %d sec%s\n",
COLOR_WHITE, $fileName, $formattedSize, $daysLeft, $hoursLeft, $minutesLeft, $secondsLeft, COLOR_RESET);
} else {
echo sprintf("%s%-40s %-15s %-1s%s\n",
COLOR_RED, $fileName, $formattedSize, "Eligible for deletion", COLOR_RESET);
}
}
}
echo str_repeat("-", 85) . "\n";
sleep(10);
}
}
// Start monitoring with 60-day threshold
displayTimeLeftForFiles($uploadDir, 60 * 24 * 60 * 60); // 60 days