Images in Directory

Discussion in 'PHP' started by darkblade, Mar 12, 2007.

  1. #1
    Ok I have a program which stores all the images in a give directory in a array. I need to make sure that the array has no other files like text files or thumb.db. How can I filter out everything except image files?
    Can this also be use to instead of image to use for folders also?
     
    darkblade, Mar 12, 2007 IP
  2. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    // This would be the array you got from your "images in the directory" function
    $files = array("thing.gif", "another.jpg", "somethingelse.jpeg", "shouldnotbehere.txt", "yetanother.png", "thumbs.db", "more.bmp");
    
    $files = array_filter($files, "imageOnly");
    echo "<xmp>";
    print_r($files);
    echo "</xmp>";
    
    function imageOnly($input) {
       return preg_match('/\.(gif|jpe?g|png|bmp)$/', $input);
    }
    
    ?>
    PHP:
    For that you would do:

    $files = array_filter($files, "is_dir");
    PHP:
     
    Robert Plank, Mar 12, 2007 IP
  3. darkblade

    darkblade Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK, cool, just have one more question. How do I do a case insensitive comparison?
    Iam trying to sort the array, but the cases of the files change the order.
     
    darkblade, Mar 12, 2007 IP
  4. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    natcasesort($files);
    PHP:
     
    Robert Plank, Mar 13, 2007 IP