<?php
include 'auth.php';
require_permission('videos_delete');
include '../includes/db.php';
if (isset($_GET['id'])) {
$id = $_GET['id'];
// Get video and thumbnail filenames
$stmt_select = $conn->prepare("SELECT video_file, thumbnail FROM videos WHERE id = ?");
$stmt_select->bind_param("i", $id);
$stmt_select->execute();
$result = $stmt_select->get_result();
if ($result->num_rows === 1) {
$video = $result->fetch_assoc();
$video_file_to_delete = $video['video_file'];
$thumbnail_to_delete = $video['thumbnail'];
// Delete DB record
$stmt_delete = $conn->prepare("DELETE FROM videos WHERE id = ?");
$stmt_delete->bind_param("i", $id);
if ($stmt_delete->execute()) {
// Delete video file
if (!empty($video_file_to_delete) && file_exists('../uploads/videos/' . $video_file_to_delete)) {
unlink('../uploads/videos/' . $video_file_to_delete);
}
// Delete thumbnail file
if (!empty($thumbnail_to_delete) && file_exists('../uploads/videos/' . $thumbnail_to_delete)) {
unlink('../uploads/videos/' . $thumbnail_to_delete);
}
header("Location: videos.php?success=تم حذف الفيديو بنجاح");
} else {
header("Location: videos.php?error=حدث خطأ أثناء الحذف");
}
$stmt_delete->close();
} else {
header("Location: videos.php?error=لم يتم العثور على الفيديو");
}
$stmt_select->close();
$conn->close();
} else {
header("Location: videos.php");
exit();
}
?>