<?php
session_start();
include("config.php");

if (isset($_FILES['file'])) {
    $user = $_SESSION['user_name'];
    $target = "uploads/" . time() . "_" . $_FILES['file']['name'];
    $type = strtolower(pathinfo($target, PATHINFO_EXTENSION));

    if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
        mysqli_query($conn, "INSERT INTO chat_messages (user_name, file_path, file_type) VALUES ('$user', '$target', '$type')");
    }
}
?>