如何使用 PHP 和 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>