【问题标题】:How can i show the images in order in website with PHP and HTML?如何使用 PHP 和 HTML 在网站中按顺序显示图像?
【发布时间】:2021-06-10 18:43:04
【问题描述】:

我在图片上传页面中添加了图片,并在另一个网页中显示图片和缩略图。我将调整大小名称与 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> 
        

【问题讨论】:

  • 这能回答你的问题吗? sort files by date in PHP
  • 我希望图像按照我添加它们的顺序出现在 html 页面上。当我尝试使用 sort() 函数修复我自己的代码时,我破坏了代码并且它没有显示任何结果。如何将 sort() 函数集成到我的代码中?谢谢。

标签: php html image-gallery


【解决方案1】:

由于您在关注其他 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>

【讨论】:

  • 我试过这个,但是有一个问题,它没有显示 HTML 页面上的所有图像,它只显示我最后添加的任何图像,并且在我添加它时它会改变,其他代码没有这个问题。
  • 嗯。如果您将die('&lt;pre&gt;'.print_r($thefiles,1).'&lt;/pre&gt;'); 放在ksort($thefiles); 之后,它是否只显示一项?
  • 它只显示:Array ( [0] => 1623352479.jpg )
  • 但是,我已将 4 张图片上传到该文件夹​​。它只显示我安装的最后一个。当我上传另一张图片时,即使数字是5,也只显示了数组0,这次似乎只有我添加的最后一个。
  • 我刚刚更新了答案 - 只是这一行发生了变化:$thefiles[filemtime('image/' . $file)] = $file;
猜你喜欢
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2019-07-24
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
相关资源
最近更新 更多