How to get only images using scandir in PHP?

You can use glob $images = glob(‘/tmp/*.{jpeg,gif,png}’, GLOB_BRACE); If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos, fnmatch or pathinfo to get the extension is up … Read more

scandir() to sort by date modified

function scan_dir($dir) { $ignored = array(‘.’, ‘..’, ‘.svn’, ‘.htaccess’); $files = array(); foreach (scandir($dir) as $file) { if (in_array($file, $ignored)) continue; $files[$file] = filemtime($dir . “https://stackoverflow.com/” . $file); } arsort($files); $files = array_keys($files); return ($files) ? $files : false; }