<?php

session_start();

require_once "../../config/koneksi.php";


if (!isset($_SESSION['user_id'])) {

    header("Location: ../login.php");
    exit;
}


// ambil data form

$judul = $_POST['judul'];
$deskripsi = $_POST['deskripsi'];



// cek gambar

if (!isset($_FILES['gambar']) || $_FILES['gambar']['error'] != 0) {

    echo "Gambar belum dipilih";
    exit;
}


// ambil gambar

$namaGambar = time() . "_" . $_FILES['gambar']['name'];

$tmp = $_FILES['gambar']['tmp_name'];



// folder upload

$folder = "../../uploads/galeri/";


// buat folder jika belum ada

if (!is_dir($folder)) {

    mkdir($folder, 0777, true);
}



// upload gambar

$upload = move_uploaded_file(
    $tmp,
    $folder . $namaGambar
);



if (!$upload) {

    echo "Gambar gagal diupload";
    exit;
}



// simpan database

$query = mysqli_query(

    $koneksi,

    "INSERT INTO galeri
    (
        judul,
        gambar,
        deskripsi
    )

    VALUES

    (
        '$judul',
        '$namaGambar',
        '$deskripsi'
    )"

);



if ($query) {

    header("Location:index.php");
    exit;
} else {

    echo mysqli_error($koneksi);
}
