I\'m reading all the files in a single directory and I want to filter on JPG,JPEG,GIF and PNG.
Both capital and small letters. Those are the only files to be accepte
Here are two different ways to compile an array of files by type (conf for demo) from a target directory. I'm not sure which is better performance wise.
$path = '/etc/apache2/';
$conf_files = [];
// Remove . and .. from the returned array from scandir
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $file) {
if(in_array(pathinfo($file, PATHINFO_EXTENSION), ['conf'])) {
$conf_files[] = $file;
}
}
return $conf_files;
This will return the full file path not just the file name
return $files = glob($path . '*.{conf}',GLOB_BRACE);