file($file['tmp_name']) ?: 'application/octet-stream'; $allowedMime = ['image/jpeg', 'image/png']; if (!in_array($mime, $allowedMime, true)) { error_log("Unsupported MIME type: $mime"); jsonError("Unsupported image MIME type"); exit; } // (اختياري) حد أقصى للحجم 10MB $maxBytes = 10 * 1024 * 1024; if ($file['size'] > $maxBytes) { error_log("Image too large: {$file['size']} bytes"); jsonError("Image too large (max 10MB)"); exit; } // 📁 مسارات الحفظ $uploadDir = "../uploads/documents/"; if (!is_dir($uploadDir)) { if (!mkdir($uploadDir, 0755, true) && !is_dir($uploadDir)) { error_log("Failed to create upload directory: $uploadDir"); jsonError("Server error: cannot create upload directory"); exit; } } $baseName = "driver_{$type}_{$driverId}"; $uniqueName = $baseName . "." . $extension; $uploadPath = $uploadDir . $uniqueName; // ⬆️ نقل الملف if (!move_uploaded_file($file['tmp_name'], $uploadPath)) { error_log("Failed to move uploaded file to $uploadPath"); jsonError("Failed to move uploaded image"); exit; } // 🔒 منع التنفيذ لو رُفع PHP بالخطأ @chmod($uploadPath, 0644); // 🌐 توليد BASE_URL آمن (يدعم ENV أو يعتمد على المضيف الحالي) if (!defined('BASE_URL')) { $APP_BASE_URL = rtrim(getenv('APP_BASE_URL') ?: '', '/'); if ($APP_BASE_URL === '') { $scheme = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'); $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; define('BASE_URL', $scheme . '://' . $host); } else { define('BASE_URL', $APP_BASE_URL); } } // ⚙️ مسار الرابط العام (عدّل المسار حسب نشر مشروعك) $publicPath = "/siro/auth/uploads/documents/" . $uniqueName; $imageUrl = rtrim(BASE_URL, '/') . $publicPath; // ✅ نتيجة نهائية: فقط رابط الصورة وبعض البيانات المفيدة printSuccess([ $imageUrl, ]);