0

以下のコードを使用して、フォルダーからフォト ギャラリーを生成しています。サムネイルを日付順に並べ替えるにはどうすればよいですか。

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

    ?>
4

1 に答える 1

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 として次のように使用します。

次に、ステップ 2 を開始する前に、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>';
    }

?>

于 2012-06-22T13:00:34.873 に答える