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

时间:2022-01-06 00:23:41

Here is my current php script that will display all images from a folder into a table. However, there are a lot of images and the load time is really slow. How can I limit the number of images displayed and create a dynamic navigation feature? Thanks!!

这是我当前的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 个解决方案

#1


0  

You could also save the $files variable in the $_SESSION array. Something like

您还可以将$ 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'];
}

Then check the size of the $files and create links based on the size you want.

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

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

Then use the $_GET['page'] variable to determine the desired set of images to display.

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

#1


0  

You could also save the $files variable in the $_SESSION array. Something like

您还可以将$ 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'];
}

Then check the size of the $files and create links based on the size you want.

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

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

Then use the $_GET['page'] variable to determine the desired set of images to display.

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