<?php
session_start();
$destdir = __DIR__ . '/../confidential/uploads/';
$status = '';

if (empty($_SESSION['username'])) exit('Access denied.');

if (!empty($_FILES['file'])) {
    $tmpName = $_FILES['file']['tmp_name'];
    $fileName = basename($_FILES['file']['name']);

    if (preg_match('/\.(txt)$/i', $fileName)) {
        if (is_uploaded_file($tmpName)) {
            if (move_uploaded_file($tmpName, $destdir . $fileName)) {
                $status = "<div class='text-success'>File uploaded!</div>";
            } else {
                $status = "<div class='text-danger'>An error occurred.</div>";
            }
        } else {
            $status = "<div class='text-danger'>An error occurred.</div>";
        }
    } else {
        $status = "<div class='text-danger'>Invalid file type!</div>";
    }
}

echo $status;
?>
