我怎样才能在 PHP 和 HTML 的网站上按顺序显示图片?

How can i show the images in order in website with PHP and HTML?

我添加了带有图片上传页面的图片,并在另一个网页中显示图片和缩略图。我将调整大小名称与 time() 函数一起使用。我想要这样:我希望我添加的每张图片都按照我刚刚给出的名称按顺序出现在 html 页面上。然而,当我用我写的代码添加照片时,我的 html 页面中的顺序被扭曲了,但我总是希望它按照我添加的顺序继续。如何在 html 页面上按顺序显示使用 Time() 函数重命名的图片?

home.php

                <div class ="gallery-items">
            <?php 
            $image_extensions = array("png","jpg","jpeg","gif");
            $dir = 'image/';
            if (is_dir($dir)){
                if ($dh = opendir($dir)){
                    while (($file = readdir($dh)) !== false){
                        if($file != '' && $file != '.' && $file != '..'){
                            $thumbnail_path = "image/thumbnailkamu/".$file;
                            $image_path = "image/".$file;
                            $thumbnail_ext = pathinfo($thumbnail_path, PATHINFO_EXTENSION);
                            $image_ext = pathinfo($image_path, PATHINFO_EXTENSION);
                if (array_key_exists('delete_file', $_POST)) {
                $filen = $_POST['delete_file'];
                if (file_exists("image/".$filen)){
                            unlink("image/".$filen);
                    unlink("image/thumbnailkamu/".$filen);

                             
                        }
                     }

                            if(!is_dir($image_path) && 
                                in_array($thumbnail_ext,$image_extensions) && 
                                in_array($image_ext,$image_extensions)){
                                ?>
                <div class ="item">
                                    <a href="<?= $image_path; ?>" data-lightbox="mygallery">
                                        <img src="<?= $thumbnail_path; ?>">
                                    </a>
                                    <a class="btn btn-primary" href="<?= $image_path; ?>" download>Download</a>
                    <form action = "" method="post">
                        <input type="hidden" value="<?= $file; ?>" name="delete_file" />
                        <input class = "button" type="submit"  value="Delete" />
                    </form>
                                </div>
                                <?php
                            }   
                        }
                        
                    }
                }
                    closedir($dh);
                }
            ?>
        </div> 
        

因为您无法关注另一个 S.O。回答,试试这个。首先将文件放在一个数组中,filemtime 作为 key,然后在开始迭代之前使用 ksort 对其进行排序

<div class="gallery-items">
    <?php
$image_extensions = array("png", "jpg", "jpeg", "gif");
$dir = 'image/';
if (is_dir($dir)) {

    $thefiles = [];
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "" && $file != "." && $file != "..") {
                $thefiles[filemtime('image/' . $file)] = $file;
            }
        }
        closedir($dh);
    }
    // sort the by most recent at the top
    ksort($thefiles);

    // now loop through
    foreach ($thefiles as $file) {
        $thumbnail_path = "image/thumbnailkamu/" . $file;
        $image_path = "image/" . $file;
        $thumbnail_ext = pathinfo($thumbnail_path, PATHINFO_EXTENSION);
        $image_ext = pathinfo($image_path, PATHINFO_EXTENSION);
        if (array_key_exists('delete_file', $_POST)) {
            $filen = $_POST['delete_file'];
            if (file_exists("image/" . $filen)) {
                unlink("image/" . $filen);
                unlink("image/thumbnailkamu/" . $filen);

            }
        }

        if (!is_dir($image_path) &&
            in_array($thumbnail_ext, $image_extensions) &&
            in_array($image_ext, $image_extensions)) {
            ?>
    <div class="item">
        <a href="<?=$image_path;?>" data-lightbox="mygallery">
            <img src="<?=$thumbnail_path;?>">
        </a>
        <a class="btn btn-primary" href="<?=$image_path;?>" download>Download</a>
        <form action="" method="post">
            <input type="hidden" value="<?=$file;?>" name="delete_file" />
            <input class="button" type="submit" value="Delete" />
        </form>
    </div>
    <?php
}
    }

}

?>
</div>