<?php
include "koneksi.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $alt_text = mysqli_real_escape_string($koneksi, $_POST['alt_text']);

    // Pastikan folder penyimpanan ada
    $upload_dir = "../images/slider_banner";
    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0777, true);
    }

    // Proses Upload Gambar
    $gambar_name = $_FILES['gambar']['name'];
    $gambar_tmp = $_FILES['gambar']['tmp_name'];
    $gambar_path = $upload_dir . "/" . $gambar_name;

    if (move_uploaded_file($gambar_tmp, $gambar_path)) {
        // Simpan ke database
        $query = "INSERT INTO slider_banner (gambar, alt_text) VALUES ('$gambar_path', '$alt_text')";
        if (mysqli_query($koneksi, $query)) {
            header("Location: index.php?page=atur_slider&notif=berhasil_tambah_data");
            exit();
        } else {
            echo "Error: " . mysqli_error($koneksi);
        }
    } else {
        echo "Gagal mengupload gambar.";
    }
}
?>