PHP 脚本:显示文件夹中的图像,并带有限制和<下一步>导航


PHP script: Display images from folder with limit and <next> navigation

这是我当前的php脚本,它将文件夹中的所有图像显示到表中。但是,有很多图像,加载时间真的很慢。如何限制显示的图像数量并创建动态导航功能?谢谢!!

<?php
$files = glob("images/*.*");
for ($i = 1; $i < count($files); $i++) {
    $image = $files[$i];
    $supported_file = array(
        'gif',
        'jpg',
        'jpeg',
        'png'
    );
    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
        echo '<td><a href="' . $image . '"><img src="' . $image . '" alt="Random image" width=200 /></a></td>';
    }
    if ($i % 5 === 0) {
        echo "</tr><tr>";
    }
}
?>
您还可以

$files变量保存在$_SESSION数组中。类似的东西

if (empty($_SESSION['files'])) {
  $files = glob("images/*.*");
  ... code for cleaning the $files array based on extension ...
  $_SESSION['files'] = $files;
} else {
  $files = $_SESSION['files'];
}

然后检查$files的大小并根据所需的大小创建链接。

<a href="/whatever?page=2">2</a>

然后使用 $_GET['page'] 变量确定要显示的所需图像集。