1#!/usr/bin/env php
2<?php
3/**
4 * Cleanup script for /clients/dl/ temp attachment folder
5 * Deletes any file older than 24 hours based on file modification time
6 * Run daily via cron:
7 * 0 3 * * * /opt/cpanel/ea-php81/root/usr/bin/php -q /home/theservice4u/public_html/clients/crons/cleanup_dl.php
8 */
9
10$dlDir = '/home/theservice4u/public_html/clients/dl/';
11$maxAge = 86400; // 24 hours in seconds
12$now = time();
13$deleted = 0;
14$errors = 0;
15
16if (!is_dir($dlDir)) {
17 echo "[" . date('Y-m-d H:i:s') . "] ERROR: Directory not found: {$dlDir}\n";
18 exit(1);
19}
20
21$files = glob($dlDir . '*');
22
23if (empty($files)) {
24 echo "[" . date('Y-m-d H:i:s') . "] Nothing to clean up.\n";
25 exit(0);
26}
27
28foreach ($files as $file) {
29 // Skip directories and this script itself
30 if (!is_file($file) || basename($file) === 'cleanup_dl.php') {
31 continue;
32 }
33
34 $age = $now - filemtime($file);
35
36 if ($age > $maxAge) {
37 if (unlink($file)) {
38 echo "[" . date('Y-m-d H:i:s') . "] Deleted: " . basename($file) . " (age: " . round($age / 3600, 1) . "hrs)\n";
39 $deleted++;
40 } else {
41 echo "[" . date('Y-m-d H:i:s') . "] ERROR: Could not delete: " . basename($file) . "\n";
42 $errors++;
43 }
44 }
45}
46
47echo "[" . date('Y-m-d H:i:s') . "] Done. Deleted: {$deleted} file(s), Errors: {$errors}.\n";
48