$uploadDir = __DIR__ . '/ftpuploads/'; $qrDir = __DIR__ . '/ftpuploads/qr/'; $cutoffTime = time() - (5 * 60); // Calculate timestamp for 5 minutes ago function deleteOldFiles($dir, $cutoff) { $deletedCount = 0; if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) { if ($file === '.' || $file === '..') { continue; } $filePath = $dir . '/' . $file; if (is_file($filePath) && (strtolower(pathinfo($filePath, PATHINFO_EXTENSION)) === 'jpg' || strtolower(pathinfo($filePath, PATHINFO_EXTENSION)) === 'png')) { if (filemtime($filePath) < $cutoff) { if (unlink($filePath)) { $deletedCount++; // Optionally log the deleted file: // error_log("Deleted old file: " . $filePath); } else { error_log("Error deleting file: " . $filePath); } } } } } return $deletedCount; } // Call the function to delete old files $deletedUploads = deleteOldFiles($uploadDir, $cutoffTime); $deletedQrCodes = deleteOldFiles($qrDir, $cutoffTime); // Optionally, you can log the number of deleted files // error_log("Deleted " . $deletedUploads . " old uploads and " . $deletedQrCodes . " old QR codes.");