如何明智地排序照片库脚本

时间:2012-06-22 12:35:59

标签: php sorting photo photo-gallery

我使用下面的代码从文件夹生成照片库。 我如何才能明确地对缩略图进行排序。

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

    ?>

1 个答案:

答案 0 :(得分:0)

直接点击你的问题,当你正在阅读文件的目录时,你可以使用一些原生的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()这样的简单数组排序函数在循环之外对它们进行排序,然后再开始第二步。

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

测试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>';
    }

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

相关问题