PK

ADDRLIN : C:/Inetpub/vhosts/dharmayatra.co.in/httpdocs/admin/gallery/
FLL :
Current File : C:/Inetpub/vhosts/dharmayatra.co.in/httpdocs/admin/gallery/add.php

<?php
include('../includes/auth.php');
include('../../includes/db.php');

// ✅ HANDLE FORM FIRST (VERY IMPORTANT)
if(isset($_POST['submit'])){

    $uploadDir = "../../uploads/";

    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    foreach ($_FILES['images']['name'] as $key => $imageName) {

        $tmp = $_FILES['images']['tmp_name'][$key];

        if (!empty($imageName)) {

            // ✅ Get extension safely
            $ext = strtolower(pathinfo($imageName, PATHINFO_EXTENSION));

            // ✅ Allow only safe formats
            $allowed = ['jpg','jpeg','png','webp'];

            if(in_array($ext, $allowed)){

                // ✅ Unique file name
                $newName = time() . "_" . rand(1000,9999) . "." . $ext;

                move_uploaded_file($tmp, $uploadDir . $newName);

                mysqli_query($conn, "INSERT INTO gallery(image) VALUES('$newName')");
            }
        }
    }

    header("Location: index.php");
    exit; // ✅ MUST ADD
}

// ✅ LOAD UI AFTER PROCESSING
include('../includes/header.php');
include('../includes/sidebar.php');
?>

<div class="top-bar">
    <h2>➕ Add Gallery Image</h2>
</div>

<div class="card">
<form method="POST" enctype="multipart/form-data">

    <div class="form-group">
        <label>Select Images</label>
        <input type="file" name="images[]" multiple required onchange="previewImages(event)">
    </div>

    <div id="previewContainer" style="display:flex; gap:10px; flex-wrap:wrap; margin-top:10px;"></div>

    <br><br>

    <button name="submit" class="btn btn-primary">
        <i class="fa fa-upload"></i> Upload
    </button>

    <a href="index.php" class="btn">Cancel</a>

</form>
</div>

<script>
function previewImages(event){
    const container = document.getElementById('previewContainer');
    container.innerHTML = "";

    const files = event.target.files;

    for(let i=0; i<files.length; i++){
        const reader = new FileReader();

        reader.onload = function(e){
            const img = document.createElement("img");
            img.src = e.target.result;
            img.style.width = "120px";
            img.style.height = "100px";
            img.style.objectFit = "cover";
            img.style.borderRadius = "8px";

            container.appendChild(img);
        }

        reader.readAsDataURL(files[i]);
    }
}
</script>

<?php include('../includes/footer.php'); ?>


PK 99