<?php
include 'db.php';
session_start();
if (!isset($_SESSION['user'])) {
    echo "<a href='login.php'>Login first</a>";
    exit;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $access = $_POST['access'];
    $comment = $_POST['comment'];
    $file = $_FILES['file']['name'];
    $tmp = $_FILES['file']['tmp_name'];
    $path = 'uploads/' . basename($file);
    if (move_uploaded_file($tmp, $path)) {
        $name = $_SESSION['user'];
        $email = $_SESSION['email'];
        mysqli_query($conn, "INSERT INTO image(filename, access_type, comment, user_name, user_email) VALUES('$file','$access','$comment','$name','$email')");
        echo "File uploaded successfully!";
    } else {
        echo "Upload failed!";
    }
}
?>
<form method="POST" enctype="multipart/form-data">
    <select name="access">
        <option value="public">Public</option>
        <option value="private">Private</option>
    </select><br>
    <input type="file" name="file" required><br>
    <textarea name="comment" placeholder="Description"></textarea><br>
    <button type="submit">Upload</button>
</form>
<br><a href='display.php'>View Uploaded Images</a> | <a href='logout.php'>Logout</a>