Sindbad~EG File Manager
<?php
session_start();
include 'conn.php';
include 'manage-common1.php';
// Define pagination variables
$items_per_page = 10; // Number of offers to display per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $items_per_page;
// Function to handle file upload
function handle_file_upload($file) {
$target_dir = "assets/img/offers/";
$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; // Return the actual path to the file
} else {
return "Sorry, there was an error uploading your file.";
}
}
// Add Offer Update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_offer'])) {
function sanitize($data) {
return htmlspecialchars(strip_tags(trim($data)));
}
$heading = sanitize($_POST['heading']);
$para = sanitize($_POST['para']);
$link = sanitize($_POST['link']);
$image = ''; // Initialize image variable
// Check for image upload and handle it
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$uploaded_file = handle_file_upload($_FILES['image']);
// Check for error in upload function
if (strpos($uploaded_file, "Sorry") === 0) { // Error message returned from upload function
echo "<p class='text-danger'>$uploaded_file</p>";
} else {
$image = $uploaded_file; // Successfully uploaded file, store its path
}
}
// Prepare SQL for insertion into the database
$sql = "INSERT INTO offer_updates (heading, para, link, image) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss", $heading, $para, $link, $image);
// Execute the query and redirect on success
if ($stmt->execute()) {
header("Location: manage-offer-updates.php");
exit();
} else {
echo "Error: Unable to add offer update.";
}
$stmt->close();
}
// Edit Offer Update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_offer'])) {
function sanitize($data) {
return htmlspecialchars(strip_tags(trim($data)));
}
$offer_id = $_POST['edit_id'];
$heading = sanitize($_POST['edit_heading']);
$para = sanitize($_POST['edit_para']);
$link = sanitize($_POST['edit_link']);
// Initialize image variable with the current image from the database
$image = $_POST['edit_image_url'];
// Check if a new image is uploaded
if (isset($_FILES['edit_image']) && $_FILES['edit_image']['error'] === UPLOAD_ERR_OK) {
// If a new image is uploaded, handle the file upload and update the image
$uploaded_file = handle_file_upload($_FILES['edit_image']);
// Check for error in upload function
if (strpos($uploaded_file, "Sorry") === 0) { // Error message returned from upload function
echo "<p class='text-danger'>$uploaded_file</p>";
} else {
$image = $uploaded_file; // Update image if a new one is uploaded
}
}
// If image is still empty, keep the old image value
if (empty($image)) {
$image = $_POST['edit_image_url']; // Preserve the old image URL if no new image is uploaded
}
// Prepare SQL to update the offer update with new data
$sql = "UPDATE offer_updates SET heading=?, para=?, link=?, image=? WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssi", $heading, $para, $link, $image, $offer_id);
// Execute the query and redirect on success
if ($stmt->execute()) {
header("Location: manage-offer-updates.php");
exit();
} else {
echo "Error: Unable to edit offer update.";
}
$stmt->close();
}
// Delete Offer Update
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete_offer_id'])) {
$offer_id = $_GET['delete_offer_id'];
$sql = "DELETE FROM offer_updates WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $offer_id);
if ($stmt->execute()) {
header("Location: manage-offer-updates.php");
exit();
} else {
echo "Error: Unable to delete offer update.";
}
$stmt->close();
}
// Fetch Offer Updates for Display
$total_offers_result = $conn->query("SELECT COUNT(*) as total FROM offer_updates");
$total_offers = $total_offers_result->fetch_assoc()['total'];
$total_pages = ceil($total_offers / $items_per_page);
$sql = "SELECT * FROM offer_updates LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $items_per_page, $offset);
$stmt->execute();
$offers_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>Offer Updates Management</title>
</head>
<body>
<?php include 'manage-nav.php';?>
<!-- Add Offer Update 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 Offer Update</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-offer-updates.php" enctype="multipart/form-data">
<div class="mb-3">
<label for="heading" class="form-label">Heading</label>
<input type="text" id="heading" name="heading" class="form-control" required>
</div>
<div class="mb-3">
<label for="para" class="form-label">Paragraph</label>
<textarea id="para" name="para" class="form-control" rows="4" required></textarea>
</div>
<div class="mb-3">
<label for="link" class="form-label">Link</label>
<input type="url" id="link" name="link" class="form-control" required>
</div>
<div class="mb-3">
<label for="image" class="form-label">Image</label>
<input type="file" id="image" name="image" class="form-control" accept="image/*" required>
</div>
<button type="submit" name="add_offer" class="btn btn-primary">Add Offer Update</button>
</form>
</div>
</div>
</div>
</div>
<!-- Edit Offer Update 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 Offer Update</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-offer-updates.php" enctype="multipart/form-data">
<input type="hidden" id="edit_id" name="edit_id">
<div class="mb-3">
<label for="edit_heading" class="form-label">Heading</label>
<input type="text" id="edit_heading" name="edit_heading" class="form-control" required>
</div>
<div class="mb-3">
<label for="edit_para" class="form-label">Paragraph</label>
<textarea id="edit_para" name="edit_para" class="form-control" rows="4" required></textarea>
</div>
<div class="mb-3">
<label for="edit_link" class="form-label">Link</label>
<input type="url" id="edit_link" name="edit_link" class="form-control" required>
</div>
<div class="mb-3">
<label for="edit_image" class="form-label">Image</label>
<input type="file" id="edit_image" name="edit_image" class="form-control" accept="image/*">
</div>
<input type="hidden" id="edit_image_url" name="edit_image_url">
<button type="submit" name="edit_offer" class="btn btn-warning">Save Changes</button>
</form>
</div>
</div>
</div>
</div>
<!-- Offer Updates Table -->
<div class="container mt-5">
<h2>Offer Updates Management</h2>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">Add New Offer Update</button>
<table class="table table-striped mt-3">
<thead>
<tr>
<th>Heading</th>
<th>Paragraph</th>
<th>Link</th>
<th>Image</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while ($offer = $offers_result->fetch_assoc()) { ?>
<tr>
<td><?= htmlspecialchars($offer['heading']) ?></td>
<td><?= htmlspecialchars($offer['para']) ?></td>
<td><?= htmlspecialchars($offer['link']) ?></td>
<td>
<?php if (!empty($offer['image'])) { ?>
<img src="<?= htmlspecialchars($offer['image']) ?>" alt="<?= htmlspecialchars($offer['heading']) ?>" width="100">
<?php } else { ?>
<span>No image</span>
<?php } ?>
</td>
<td>
<button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#editModal" onclick="populateEditModal(<?= $offer['id'] ?>, '<?= addslashes($offer['heading']) ?>', '<?= addslashes($offer['para']) ?>', '<?= addslashes($offer['link']) ?>', '<?= addslashes($offer['image']) ?>')">
Edit
</button>
<a href="?delete_offer_id=<?= $offer['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this offer?')">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<!-- Pagination -->
<nav>
<ul class="pagination">
<li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
<a class="page-link" href="?page=<?= ($page - 1) ?>">Previous</a>
</li>
<li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
<a class="page-link" href="?page=<?= ($page + 1) ?>">Next</a>
</li>
</ul>
</nav>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function populateEditModal(id, heading, para, link, image) {
document.getElementById("edit_id").value = id;
document.getElementById("edit_heading").value = heading;
document.getElementById("edit_para").value = para;
document.getElementById("edit_link").value = link;
document.getElementById("edit_image_url").value = image;
if (image) {
document.getElementById("edit_image").setAttribute("disabled", "disabled");
}
}
</script>
</body>
</html>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists