🤩 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_services.php
<?php require_once 'header.php'; $msg = ''; // Add Service if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'add') { $title = $_POST['title']; $description = $_POST['description']; // File upload logic $image = ''; if (!empty($_FILES['image']['name'])) { $upload_dir = '../uploads/services/'; 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 services (title, description, image) VALUES (?, ?, ?)"); $stmt->execute([$title, $description, $image]); $msg = "<div class='alert alert-success'>Service added successfully.</div>"; } catch(Exception $e) { $msg = "<div class='alert alert-danger'>Error adding service.</div>"; } } // Edit Service if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'edit') { $id = $_POST['id']; $title = $_POST['title']; $description = $_POST['description']; $is_active = isset($_POST['is_active']) ? 1 : 0; // File upload logic $image_query_part = ""; $params = [$title, $description, $is_active]; if (!empty($_FILES['image']['name'])) { $upload_dir = '../uploads/services/'; if(!is_dir($upload_dir)) @mkdir($upload_dir, 0777, true); $image = time() . '_' . basename($_FILES['image']['name']); if (move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir . $image)) { $image_query_part = ", image = ?"; $params[] = $image; } } $params[] = $id; try { $stmt = $pdo->prepare("UPDATE services SET title = ?, description = ?, is_active = ? $image_query_part WHERE id = ?"); $stmt->execute($params); $msg = "<div class='alert alert-success'>Service updated successfully.</div>"; } catch(Exception $e) { $msg = "<div class='alert alert-danger'>Error updating service.</div>"; } } // Delete Service if (isset($_GET['delete'])) { $id = $_GET['delete']; try { $stmt = $pdo->prepare("DELETE FROM services WHERE id = ?"); $stmt->execute([$id]); $msg = "<div class='alert alert-success'>Service deleted successfully.</div>"; } catch(Exception $e) { $msg = "<div class='alert alert-danger'>Error deleting service.</div>"; } } // Fetch Services try { $stmt = $pdo->query("SELECT * FROM services ORDER BY id DESC"); $services = $stmt->fetchAll(); } catch (PDOException $e) { if ($e->getCode() == '42S02') { $pdo->exec("CREATE TABLE IF NOT EXISTS `services` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(150) NOT NULL, `description` text 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"); // Insert default services data $pdo->exec("INSERT INTO `services` (`title`, `description`, `image`, `is_active`) VALUES ('Aluminium Pantry Cupboard', 'Premium quality, rust-proof aluminium pantry cupboards customized to fit your kitchen space perfectly with modern luxury finishes.', 'pantry.jpg', 1), ('TV Wall Design', 'Modern TV wall units incorporating LED lighting, floating shelves, and high-end materials to elevate your living room.', 'tv-wall.jpg', 1), ('Wall Decoration', 'Aesthetic and durable wall decorations using advanced cladding and interior materials.', 'wall-deco.jpg', 1), ('Clothing Wardrobe', 'Spacious, elegantly designed aluminium and glass wardrobes tailored for maximum storage and style.', 'wardrobe.jpg', 1), ('Partition Decoration Wall', 'Stylish glass and aluminium partition walls to divide spaces while maintaining an open, modern feel.', 'partition.jpg', 1), ('Granite Works', 'High-quality granite countertops and fittings for kitchens, bathrooms, and commercial spaces.', 'granite.jpg', 1), ('Island Table', 'Custom-built kitchen island tables providing extra workspace and a contemporary focal point for your kitchen.', 'island-table.jpg', 1), ('Bar Counter', 'Luxurious bar counters for home or commercial use, designed with premium finishes and ambient lighting.', 'bar-counter.jpg', 1), ('Dressing Table', 'Modern dressing tables featuring elegant mirrors, built-in lighting, and sleek storage solutions.', 'dressing-table.jpg', 1)"); $stmt = $pdo->query("SELECT * FROM services ORDER BY id DESC"); $services = $stmt->fetchAll(); } else { $msg = "<div class='alert alert-danger'>Database Error: " . htmlspecialchars($e->getMessage()) . "</div>"; $services = []; } } ?> <div class="d-flex justify-content-between align-items-center mb-4"> <h2>Manage Services</h2> <button class="btn btn-gold" data-bs-toggle="modal" data-bs-target="#addModal"><i class="fas fa-plus me-2"></i> Add Service</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>ID</th> <th>Image</th> <th>Title</th> <th>Description</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($services as $s): ?> <tr> <td><?= $s['id'] ?></td> <td> <?php if($s['image']): ?> <img src="../uploads/services/<?= e($s['image']) ?>" alt="img" style="width: 50px; height: 50px; object-fit: cover; border-radius: 5px;"> <?php else: ?> <span class="text-muted">No image</span> <?php endif; ?> </td> <td><?= e($s['title']) ?></td> <td><?= substr(e($s['description']), 0, 50) ?>...</td> <td> <?php if($s['is_active']): ?> <span class="badge bg-success">Active</span> <?php else: ?> <span class="badge bg-danger">Inactive</span> <?php endif; ?> </td> <td> <button type="button" class="btn btn-sm btn-outline-primary me-1" data-bs-toggle="modal" data-bs-target="#editModal<?= $s['id'] ?>"> <i class="fas fa-edit"></i> </button> <a href="?delete=<?= $s['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this service?');"><i class="fas fa-trash"></i></a> <!-- Edit Modal --> <div class="modal fade" id="editModal<?= $s['id'] ?>" tabindex="-1"> <div class="modal-dialog"> <form class="modal-content" method="POST" enctype="multipart/form-data"> <div class="modal-header"> <h5 class="modal-title text-dark">Edit Service</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body text-start"> <input type="hidden" name="action" value="edit"> <input type="hidden" name="id" value="<?= $s['id'] ?>"> <div class="mb-3"> <label class="form-label text-dark">Service Title</label> <input type="text" name="title" class="form-control" value="<?= e($s['title']) ?>" required> </div> <div class="mb-3"> <label class="form-label text-dark">Image (Leave blank to keep current)</label> <input type="file" name="image" class="form-control" accept="image/*"> <?php if($s['image']): ?> <div class="mt-2 text-muted small">Current image: <?= e($s['image']) ?></div> <?php endif; ?> </div> <div class="mb-3"> <label class="form-label text-dark">Description</label> <textarea name="description" class="form-control" rows="4" required><?= e($s['description']) ?></textarea> </div> <div class="mb-3 form-check"> <input type="checkbox" name="is_active" class="form-check-input" id="isActive<?= $s['id'] ?>" <?= $s['is_active'] ? 'checked' : '' ?>> <label class="form-check-label text-dark" for="isActive<?= $s['id'] ?>">Active</label> </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-primary">Update Service</button> </div> </form> </div> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <!-- Add Modal --> <div class="modal fade" id="addModal" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <form method="POST" enctype="multipart/form-data"> <div class="modal-header"> <h5 class="modal-title">Add New Service</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">Service Title</label> <input type="text" name="title" class="form-control" required> </div> <div class="mb-3"> <label class="form-label">Image</label> <input type="file" name="image" class="form-control" accept="image/*"> </div> <div class="mb-3"> <label class="form-label">Description</label> <textarea name="description" class="form-control" rows="4" required></textarea> </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">Save Service</button> </div> </form> </div> </div> </div> <?php require_once 'footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder