<?php
function uploadFile($file, $destinationDir, $allowedExtensions, $maxSize) {
    if ($file['error'] !== UPLOAD_ERR_OK) {
        return ['success' => false, 'error' => 'File upload error: ' . $file['error']];
    }
    
    if ($file['size'] > $maxSize) {
        return ['success' => false, 'error' => 'File exceeds maximum size.'];
    }
    
    $fileInfo = pathinfo($file['name']);
    $extension = strtolower($fileInfo['extension'] ?? '');
    
    if (!in_array($extension, $allowedExtensions)) {
        return ['success' => false, 'error' => 'Invalid file extension.'];
    }
    
    $newName = uniqid() . '_' . time() . '.' . $extension;
    $destination = rtrim($destinationDir, '/') . '/' . $newName;
    
    if (move_uploaded_file($file['tmp_name'], $destination)) {
        return ['success' => true, 'filename' => $newName];
    }
    
    return ['success' => false, 'error' => 'Failed to move uploaded file.'];
}
