PHP脚本:显示带有限制和<next>导航的文件夹中的图像

时间:2016-02-06 17:06:33

标签: javascript php

这是我当前的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>";
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您还可以将$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']变量确定要显示的所需图像集。