Sindbad~EG File Manager
<?php
session_start();
include 'conn.php';
include 'manage-common1.php';
// Define pagination variables
$items_per_page = 10; // Number of categories to display per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $items_per_page;
function handle_file_upload($file) {
$target_dir = "assets/img/categories/";
$date_prefix = date("d-m-y-H-i-s"); // Add unique name to avoid overwriting
$original_name = basename($file["name"]);
$file_extension = strtolower(pathinfo($original_name, PATHINFO_EXTENSION));
$new_file_name = $date_prefix . '-' . $original_name;
$target_file = $target_dir . $new_file_name;
// Check if file is an image
$check = getimagesize($file["tmp_name"]);
if ($check === false) {
return "File is not an image.";
}
// Check file size (limit to 5MB)
if ($file["size"] > 5000000) {
return "Sorry, your file is too large.";
}
// Allow certain file formats (jpg, jpeg, png, gif)
if (!in_array($file_extension, ["jpg", "jpeg", "png", "gif"])) {
return "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
// Attempt to move the uploaded file
if (move_uploaded_file($file["tmp_name"], $target_file)) {
return $target_file;
} else {
return "Sorry, there was an error uploading your file.";
}
}
// Add Category
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_category'])) {
function sanitize($data) {
return htmlspecialchars(strip_tags(trim($data)));
}
$name = sanitize($_POST['name']);
$image_url = '';
if (isset($_FILES['image_url']) && $_FILES['image_url']['error'] === UPLOAD_ERR_OK) {
$uploaded_file = handle_file_upload($_FILES['image_url']);
if (strpos($uploaded_file, "Sorry") === 0) { // Error occurred
echo "<p class='text-danger'>$uploaded_file</p>";
} else {
$image_url = $uploaded_file;
}
}
$sql = "INSERT INTO categories (name, image_url) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $name, $image_url);
if ($stmt->execute()) {
header("Location: manage-categories.php");
exit();
} else {
echo "Error: Unable to add category.";
}
$stmt->close();
}
// Edit Category
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_category'])) {
function sanitize($data) {
return htmlspecialchars(strip_tags(trim($data)));
}
$category_id = $_POST['edit_id'];
$name = sanitize($_POST['edit_name']);
$image_url = htmlspecialchars($_POST['edit_image_url']);
if (isset($_FILES['edit_image_url']) && $_FILES['edit_image_url']['error'] === UPLOAD_ERR_OK) {
$uploaded_file = handle_file_upload($_FILES['edit_image_url']);
if (strpos($uploaded_file, "Sorry") === 0) { // Error occurred
echo "<p class='text-danger'>$uploaded_file</p>";
} else {
$image_url = $uploaded_file;
}
}
$sql = "UPDATE categories SET name=?, image_url=? WHERE category_id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssi", $name, $image_url, $category_id);
if ($stmt->execute()) {
header("Location: manage-categories.php");
exit();
} else {
echo "Error: Unable to edit category.";
}
$stmt->close();
}
// Delete Category
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete_category_id'])) {
$category_id = $_GET['delete_category_id'];
$sql = "DELETE FROM categories WHERE category_id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $category_id);
if ($stmt->execute()) {
header("Location: manage-categories.php");
exit();
} else {
echo "Error: Unable to delete category.";
}
$stmt->close();
}
// Fetch Categories for Display
$total_categories_result = $conn->query("SELECT COUNT(*) as total FROM categories");
$total_categories = $total_categories_result->fetch_assoc()['total'];
$total_pages = ceil($total_categories / $items_per_page);
$sql = "SELECT * FROM categories LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $items_per_page, $offset);
$stmt->execute();
$categories_result = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Category Management</title>
</head>
<body>
<?php include 'manage-nav.php';?>
<!-- Add Category Modal -->
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addModalLabel">Add Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="manage-categories.php" enctype="multipart/form-data">
<div class="mb-3">
<label for="name" class="form-label">Category Name</label>
<input type="text" id="name" name="name" class="form-control" required>
</div>
<div class="mb-3">
<label for="image_url" class="form-label">Image</label>
<input type="file" id="image_url" name="image_url" class="form-control" accept="image/*">
</div>
<button type="submit" name="add_category" class="btn btn-primary">Add Category</button>
</form>
</div>
</div>
</div>
</div>
<!-- Edit Category Modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="editModalLabel">Edit Category</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="manage-categories.php" enctype="multipart/form-data">
<input type="hidden" id="edit_id" name="edit_id">
<div class="mb-3">
<label for="edit_name" class="form-label">Category Name</label>
<input type="text" id="edit_name" name="edit_name" class="form-control" required>
</div>
<div class="mb-3">
<label for="edit_image_url" class="form-label">Image</label>
<input type="file" id="edit_image_url" name="edit_image_url" class="form-control" accept="image/*">
</div>
<button type="submit" name="edit_category" class="btn btn-primary">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
<div class="container mt-5">
<h1 class="mb-4">Category Management</h1>
<!-- Add New Category Button, only visible if logged in -->
<?php if ($logged_in) { ?>
<button type="button" class="btn btn-primary mb-4" data-bs-toggle="modal" data-bs-target="#addModal">
Add New Category
</button>
<?php } ?>
<table class="table">
<thead>
<tr>
<th>Category Name</th>
<th>Image</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while ($category = $categories_result->fetch_assoc()) { ?>
<tr>
<td><?= htmlspecialchars($category['name']) ?></td>
<td>
<?php if (!empty($category['image_url'])) { ?>
<img src="<?= htmlspecialchars($category['image_url']) ?>" alt="<?= htmlspecialchars($category['name']) ?>" width="100">
<?php } ?>
</td>
<td>
<?php if ($logged_in) { ?>
<button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#editModal" onclick="populateEditModal(<?= $category['category_id'] ?>, '<?= addslashes($category['name']) ?>', '<?= addslashes($category['image_url']) ?>')">
Edit
</button>
<a href="?delete_category_id=<?= $category['category_id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this category?')">Delete</a>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<nav>
<ul class="pagination">
<li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
<a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++) { ?>
<li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
<a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
</li>
<?php } ?>
<li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
<a class="page-link" href="?page=<?= $page + 1 ?>">Next</a>
</li>
</ul>
</nav>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
window.populateEditModal = function(id, name, image_url) {
document.getElementById('edit_id').value = id;
document.getElementById('edit_name').value = name;
document.getElementById('edit_image_url').value = image_url;
}
});
</script>
<?php include 'footer-manage.php'; ?>
</body>
</html>
<?php $conn->close(); ?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists