<?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_kanan";
    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_kanan (gambar, alt_text) VALUES ('$gambar_path', '$alt_text')";
        if (mysqli_query($koneksi, $query)) {
            header("Location: index.php?page=slider_kanan&notif=berhasil_tambah_data");
            exit();
        } else {
            echo "Error: " . mysqli_error($koneksi);
        }
    } else {
        echo "Gagal mengupload gambar.";
    }
}
?>