Need some quick help on my short php script that shows images from a directory

Discussion in 'PHP' started by js09, Aug 2, 2011.

  1. #1
    I've got a basic PHP script (shown below) that simply displays all the images (jpegs) in the directory, in no particular order. I have this code in an 'index.php' file

    My issue is now I'd like to sort the images in chronological order. I tried messing with the sort() code, but couldn't get it to work.

    <?php
            $handle = opendir ('./');
            while (false !== ($file = readdir($handle))) {
                if($file != "." && $file != ".." && $file != basename(__FILE__)) {
                    echo '<img src="'.$file.'"/>';
                }
            }
    ?>
    Code (markup):
    Any help is GREATLY appreciated. Thanks!
     
    js09, Aug 2, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    try http://php.net/glob

    use it

    foreach (glob("*.png") AS $image)
    {
    // do something with image
    }
    
    Code (markup):
     
     
    EricBruggema, Aug 4, 2011 IP
  3. marleypeters

    marleypeters Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    glob does not give you chronological idea. Use filemtime() as follows:

    $last_modified = filemtime("thisfile.php");

    This function returns time in Unix timestamp. Put into array and sort ascending order, then convert to date upon displaying it.
     
    marleypeters, Aug 8, 2011 IP