View Single Post
  #4   IP: 153.99.39.110
Old 2016-08-16, 12:56 AM
Kennedyville Kennedyville is offline
初级会员
 
Join Date: 2013-05-30
Posts: 1
Kennedyville 现在声名狼藉
Default

Here is a simple way to get only images. Works with PHP >= 5.2 version. The collection of extensions are in lowercase, so making the file extension in loop to lowercase make it case insensitive.
Code:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');

// init result
$result = array();

// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');

// iterate
foreach ($directory as $fileinfo) {
    // must be a file
    if ($fileinfo->isFile()) {
        // file extension
        $extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
        // check if extension match
        if (in_array($extension, $extensions)) {
            // add to result
            $result[] = $fileinfo->getFilename();
        }
    }
}
// print result
print_r($result);
I hope this is useful if you want case insensitive and image only extensions.
Reply With Quote