简体   繁体   English

如何按日期排序照片库脚本

[英]How to sort photo gallery script date wise

I am using below code to generate photo gallery from a folder. 我正在使用以下代码从文件夹生成照片库。 How can i sort thumbnails date wise. 我如何排序明智的缩略图。

<?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>';
        }

    ?>

To hit your question directly, while you're reading the dir for files, you can get info about the files using some native php functions... 为了直接解决您的问题,在读取文件目录时,您可以使用一些本机php函数获取有关文件的信息...

When a file was last accessed: fileatime - http://www.php.net/manual/en/function.fileatime.php 上次访问文件的时间:fileatime- http ://www.php.net/manual/en/function.fileatime.php

When a file was created: filectime - http://www.php.net/manual/en/function.filectime.php 创建文件时:filectime- http ://www.php.net/manual/en/function.filectime.php

When a file was modified: filemtime - http://php.net/manual/en/function.filemtime.php 修改文件后:filemtime- http ://php.net/manual/en/function.filemtime.php

These return the time, formatted as unix time. 这些返回时间,格式为unix时间。

For simplicity, I would use filectime to find the time, and use that value as the KEY in the $files array, like so: $files[filectime($file)] = $file; 为简单起见,我将使用filectime查找时间,并将该值用作$ files数组中的KEY,例如:$ files [filectime($ file)] = $ file;

Then you can use a simple array sorting function like ksort() to order them outside the loop, before you start step two. 然后,在开始第二步之前,可以使用简单的数组排序函数(例如ksort())在循环外对其进行排序。

Now... Going slightly deeper... I would probably use a database to store information like this, instead of hitting the file system every time the page is loaded. 现在……要更深入……我可能会使用数据库来存储这样的信息,而不是每次加载页面时都要访问文件系统。 It will be a little more overhead in development, but depending on the size of the dir, could save you a lot of time and processing power. 这将在开发中增加一些开销,但是根据目录的大小,可以节省大量时间和处理能力。

TESTED 2012-06-23 测试于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>'; } ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM