【问题标题】:How to sort photo gallery script date wise如何明智地对照片库脚本日期进行排序
【发布时间】:2012-06-22 12:35:59
【问题描述】:

我正在使用以下代码从文件夹生成照片库。 如何按日期对缩略图进行排序。

<?php

        /* settings */
        $image_dir = 'photo_gallery/';
        $per_column = 6;


        /* step one:  read directory, make array of files */
        if ($handle = opendir($image_dir)) {
            while (false !== ($file = readdir($handle))) 
            {
                if ($file != '.' && $file != '..') 
                {
                    if(strstr($file,'-thumb'))
                    {
                        $files[] = $file;
                    }
                }
            }
            closedir($handle);
        }

        /* step two: loop through, format gallery */
        if(count($files))
        {


            foreach($files as $file)
            {
                $count++;
                echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
                if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
            }
        }
        else
        {
            echo '<p>There are no images in this gallery.</p>';
        }

    ?>

【问题讨论】:

    标签: php sorting photo photo-gallery


    【解决方案1】:

    要直接提出您的问题,在您阅读文件的目录时,您可以使用一些本机 php 函数获取有关文件的信息...

    上次访问文件的时间:fileatime - http://www.php.net/manual/en/function.fileatime.php

    文件创建时间:filectime - http://www.php.net/manual/en/function.filectime.php

    当文件被修改时:filemtime - http://php.net/manual/en/function.filemtime.php

    这些返回时间,格式为 unix 时间。

    为简单起见,我将使用 filectime 来查找时间,并将该值用作 $files 数组中的 KEY,如下所示:$files[filectime($file)] = $file;

    然后,您可以在开始第二步之前使用简单的数组排序函数(如 ksort())在循环外对它们进行排序。

    现在...再深入一点...我可能会使用数据库来存储这样的信息,而不是每次加载页面时都访问文件系统。这会在开发中增加一点开销,但根据 dir 的大小,可以为您节省大量时间和处理能力。

    测试 2012-06-23

        /* settings */
        $image_dir = 'photo_gallery/';
        $per_column = 6;
    
    
        /* step one:  read directory, make array of files */
        if ($handle = opendir($image_dir)) {
            while (false !== ($file = readdir($handle))) 
            {
                if ($file != '.' && $file != '..') 
                {
                    if(strstr($file,'-thumb'))
                    {
                        $files[filemtime($image_dir . $file)] = $file;
                    }
                }
            }
            closedir($handle);
        }
    
        /* step two: loop through, format gallery */
        if(count($files))
        {
            krsort($files);
    
            foreach($files as $file)
            {
                $count++;
                echo '<a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';
                if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
            }
        }
        else
        {
            echo '<p>There are no images in this gallery.</p>';
        }
    
    ?>
    

    【讨论】:

    • 谢谢,顺便说一句,我使用了你的建议,但它不起作用,你可以修改代码本身。以下代码供参考:stackoverflow.com/questions/2667065/sort-files-by-date-in-php
    • 谢谢。修改后的代码对文件进行排序,但最后上传的图像转到顶部(第一张图像)的底部。你能告诉我我应该怎么做才能先带上最后上传的图片。
    • 您的意思是顺序颠倒了,还是底部显示了一个特定的图像而应该在顶部?
    • 我现在明白了,我已经看到了标记... ksort 函数正在按 asc 排序,您想要 desc... 我已经更新了上面的代码以使用 krsort 而不是 ksort。
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2019-01-17
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多