<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
        $uploadDir = '../img/user/';
        $fileExt = ".".pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $fileName = "bilspro_".date('YmdHis').$fileExt;
        $uploadFile = $uploadDir . $fileName;
        $formId = $_POST['formId'];

        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
            // Menyimpan nama file dalam variabel session
            if (!isset($_SESSION['uploaded_files'])) {
                $_SESSION['uploaded_files'] = [];
            }
            $_SESSION['uploaded_files'][$formId] = $fileName;
            
            echo 'File successfully uploaded.';
        } else {
            echo 'Error uploading file.';
        }
    } else {
        echo 'No file uploaded or upload error.';
    }
} else {
    echo 'Invalid request method.';
}
?>