85 lines
2.6 KiB
PHP
Executable File
85 lines
2.6 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* delete_old_serial_docs.php
|
|
* يحذف صور الوثائق الأقدم من مدة محددة (افتراضي 48 ساعة) من private_uploads
|
|
* ضع الملف بجانب upload_serial_document.php ليستخدم نفس الشجرة.
|
|
*/
|
|
|
|
date_default_timezone_set('Asia/Damascus');
|
|
|
|
// === الإعدادات ===
|
|
// نفس ما في upload_serial_document.php:
|
|
const UPLOAD_ROOT = __DIR__ . "/../../private_uploads";
|
|
const ALLOWED_EXTS = ['jpg','png','webp'];
|
|
|
|
// المدة قبل الحذف (ثواني): افتراضي يومين، ويمكن تمريرها عبر CLI
|
|
$ttlSeconds = 2 * 24 * 60 * 60; // 48 ساعة
|
|
if (PHP_SAPI === 'cli' && isset($argv[1]) && ctype_digit($argv[1])) {
|
|
$ttlSeconds = (int)$argv[1];
|
|
}
|
|
|
|
// ملف لوج اختياري
|
|
$logFile = __DIR__ . '/delete_old_serial_docs.log';
|
|
$log = @fopen($logFile, 'ab');
|
|
|
|
// دالة بسيطة للّوج
|
|
$logln = function(string $msg) use ($log) {
|
|
$line = '[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL;
|
|
if ($log) @fwrite($log, $line);
|
|
};
|
|
|
|
// تحقّق أن مجلد الرفع صحيح وموجود
|
|
$root = realpath(UPLOAD_ROOT);
|
|
if ($root === false || !is_dir($root)) {
|
|
$logln("❌ UPLOAD_ROOT not found: " . UPLOAD_ROOT);
|
|
exit(1);
|
|
}
|
|
|
|
$logln("===== Start cleanup in: {$root} | TTL={$ttlSeconds}s =====");
|
|
|
|
// مُكرّر آمن عبر RecursiveIterator
|
|
$it = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
|
|
$now = time();
|
|
$deleted = 0;
|
|
$checked = 0;
|
|
|
|
// اسم الملف المتوقع: driverId__docType.ext
|
|
$docTypes = [
|
|
'driver_license_front','driver_license_back',
|
|
'car_license_front','car_license_back',
|
|
];
|
|
$docTypesRegex = implode('|', array_map('preg_quote', $docTypes));
|
|
|
|
foreach ($it as $node) {
|
|
if (!$node->isFile()) continue;
|
|
$checked++;
|
|
|
|
$path = $node->getPathname();
|
|
$ext = strtolower($node->getExtension());
|
|
|
|
// فلترة الامتدادات
|
|
if (!in_array($ext, ALLOWED_EXTS, true)) continue;
|
|
|
|
// فلترة اسم الملف (حماية من حذف ملفات أخرى)
|
|
$name = $node->getBasename();
|
|
if (!preg_match('/^[A-Za-z0-9_-]+__(' . $docTypesRegex . ')\.(jpg|png|webp)$/i', $name)) {
|
|
continue;
|
|
}
|
|
|
|
$age = $now - $node->getMTime();
|
|
if ($age >= $ttlSeconds) {
|
|
if (@unlink($path)) {
|
|
$deleted++;
|
|
$logln("🗑 Deleted: {$path} | age=" . round($age/3600, 1) . "h");
|
|
} else {
|
|
$logln("⚠️ Failed to delete: {$path}");
|
|
}
|
|
}
|
|
}
|
|
|
|
$logln("Done. checked={$checked}, deleted={$deleted}");
|
|
if ($log) @fclose($log); |