What is the best way to create an admin menu in PHP to display and delete images from a specific folder?

To create an admin menu in PHP to display and delete images from a specific folder, you can use PHP to scan the folder for images, display them in a table with options to delete, and handle the deletion process securely. You can create a simple HTML form to submit the delete request and use PHP to process the deletion based on the selected image.

<?php
// Define the folder path where images are stored
$folderPath = 'images/';

// Scan the folder for images
$images = glob($folderPath . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

// Display images in a table with delete option
echo '<table>';
foreach ($images as $image) {
    echo '<tr>';
    echo '<td><img src="' . $image . '" width="100"></td>';
    echo '<td><a href="?delete=' . basename($image) . '">Delete</a></td>';
    echo '</tr>';
}
echo '</table>';

// Handle image deletion
if (isset($_GET['delete'])) {
    $imageToDelete = $folderPath . $_GET['delete'];
    if (file_exists($imageToDelete)) {
        unlink($imageToDelete);
        echo 'Image deleted successfully.';
    } else {
        echo 'Image not found.';
    }
}
?>