簡體   English   中英

如何按日期排序照片庫腳本

[英]How to sort photo gallery script 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>';
        }

    ?>

為了直接解決您的問題,在讀取文件目錄時,您可以使用一些本機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>'; } ?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM