How can PHP implement pagination or paging to manage the display of images from a directory?

To implement pagination for displaying images from a directory in PHP, you can use a combination of functions like scandir() to get the list of images, array_slice() to slice the images based on the current page, and a simple loop to display the images. You will also need to calculate the total number of pages based on the number of images and the number of images to display per page.

<?php
$imagesPerPage = 10;
$imagesDir = 'images/';
$images = array_slice(scandir($imagesDir), 2); // Remove . and ..
$totalImages = count($images);
$totalPages = ceil($totalImages / $imagesPerPage);

$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $imagesPerPage;
$end = $start + $imagesPerPage;

for ($i = $start; $i < $end && $i < $totalImages; $i++) {
    echo '<img src="' . $imagesDir . $images[$i] . '" alt="Image">';
}

// Pagination links
for ($i = 1; $i <= $totalPages; $i++) {
    echo '<a href="?page=' . $i . '">' . $i . '</a> ';
}
?>