sorting filenames

    Date: 04/15/05 (PHP Community)    Keywords: no keywords


    I am trying to build a thumbnail list by listing the images in a folder, and sorting them by the date, which is included in the file name. So for example, if the folder contains these files:

    20050108_image1.jpg
    20050108_image2.jpg
    20050108_image3.jpg
    
    20050110_image1.jpg
    20050110_image2.jpg
    
    20050124_image1.jpg
    20050124_image2.jpg
    


    I would like to get a result similar to this:

    01-08-2005
    Other Dates: [ 01-10-2005 ] [ 01-24-2005 ]

    image1  image2  image3

    If you click on the other dates, it would, of course, show the images with those dates.

    I have tried a couple of things, including putting the images into an array, but cannot figure out how to sort the array by the date. Seems like it should be simple but I am stuck.

    Right now I am reading the directory and putting each image with an image extension into an array. I am trying to use the date as the key but not having any success.
    
    while (($file = readdir($dh)) !== false) {
        if ((!($file==".")) AND (!($file==".."))){
          $ifdir=filetype($ImageDir.$file);
          if (!($ifdir=="dir")){
           $extension = substr(strrchr($file, "."), 1);
           
           
           if (($extension == "jpg") OR ($extension=="gif") OR ($extension=="png") OR ($extension=="jpeg")){
            $date = substr($file,'0','8');
            
            $listFile[$date][]=$file;
                           
           }
          }
        }
       }
       // show files
             if(count($listFile) > 0){
                 sort($listFile);
                  $body.='
    '.print_r($listFile).'
    ' ;            }   


    Which gives me this:

    Array
    (
        [0] => Array
            (
                [0] => 20050108_image1.jpg
                [1] => 20050108_image2.jpg
                [2] => 20050108_image3.jpg
            )
    
        [1] => Array
            (
                [0] => 20050124_image1.jpg
    
                [1] => 20050124_image2.jpg
            )
    
        [2] => Array
            (
                [0] => 20050110_image1.jpg
                [1] => 20050110_image2.jpg
             )
    )
    

    I expected the array to have the date as the KEY.  Like so:

    Array
    (
        [20050108] => Array
            (
                [0] => 20050108_image1.jpg
                [1] => 20050108_image2.jpg
                [2] => 20050108_image3.jpg
            )
    
        [20050124] => Array
            (
                [0] => 20050124_image1.jpg
                [1] => 20050124_image2.jpg
            )
    
        [20050110] => Array
            (
                [0] => 20050110_image1.jpg
                [1] => 20050110_image2.jpg
             )
    )
    


    What am I doing wrong here?

    Is there a better way to achieve what I am trying to achieve?

    Source: http://www.livejournal.com/community/php/286445.html

« get file from remote site... || Webhosting »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home