I have a set of files that looks like this:
image01.png
image01.jpg
image02.png
image02.jpg
image03.png
image03.jpg
image03.gif
Can anyone think of a better way to get only one file from each set with the same basename, and based on a prioritized set of extensions? All the continue
statements in my current code looks... not so good. And I am positive I will add more filetypes to the mix, so it won't get any prettier (or easier to manage for that matter).
while(($Filename = readdir($DirHandle)) !== FALSE){
$Ext = pathinfo($Filename, PATHINFO_EXTENSION);
$Basename = basename($Filename, '.'.$Ext);
switch($Ext){
case 'png':
// highest priority, we're good
break;
case 'jpeg':
case 'jpg':
// is there a higher-priority filetype with the same basename?
if(file_exists($Dir.'/'.$Basename.'.png'))
continue 2; // then, let's proceed to the next file
break;
case 'gif':
if(file_exists($Dir.'/'.$Basename.'.png'))
continue 2;
elseif(file_exists($Dir.'/'.$Basename.'.jpeg'))
continue 2;
elseif(file_exists($Dir.'/'.$Basename.'.jpg'))
continue 2;
break;
// etc. etc...
default:
// not a filetype we're interested in at all
continue 2;
}
}
A scandir()
solution would be fine too. But even then, I personally couldn't think of any better solution..